Skip to content

Instantly share code, notes, and snippets.

@Harshmakadia
Last active February 24, 2019 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Harshmakadia/6ac425a76e8a2d6520cb5416128b861a to your computer and use it in GitHub Desktop.
Save Harshmakadia/6ac425a76e8a2d6520cb5416128b861a to your computer and use it in GitHub Desktop.
Add/Update Context
import React, { Component } from 'react';
import ShopContext from './shop-context';
class GlobalState extends Component {
state = {
products: [
{ id: 'p1', title: 'React 16 Sticker + T-shirt', price: 29.99 },
{ id: 'p2', title: 'Vue.js T-shirt', price: 9.99 },
{ id: 'p3', title: 'Angular T-shirt', price: 8.99 },
{ id: 'p4', title: 'JS Notebook', price: 2.99 }
],
cart: []
};
addProductToCart = product => {
console.log('Adding product', product);
const updatedCart = [...this.state.cart];
const updatedItemIndex = updatedCart.findIndex(
item => item.id === product.id
);
if (updatedItemIndex < 0) {
updatedCart.push({ ...product, quantity: 1 });
} else {
const updatedItem = {
...updatedCart[updatedItemIndex]
};
updatedItem.quantity++;
updatedCart[updatedItemIndex] = updatedItem;
}
this.setState({ cart: updatedCart });
};
removeProductFromCart = productId => {
console.log('Removing product with id: ' + productId);
const updatedCart = [...this.state.cart];
const updatedItemIndex = updatedCart.findIndex(
item => item.id === productId
);
const updatedItem = {
...updatedCart[updatedItemIndex]
};
updatedItem.quantity--;
if (updatedItem.quantity <= 0) {
updatedCart.splice(updatedItemIndex, 1);
} else {
updatedCart[updatedItemIndex] = updatedItem;
}
this.setState({ cart: updatedCart });
};
render() {
return (
<ShopContext.Provider
value={{
products: this.state.products,
cart: this.state.cart,
addProductToCart: this.addProductToCart,
removeProductFromCart: this.removeProductFromCart
}}
>
{this.props.children}
</ShopContext.Provider>
);
}
}
export default GlobalState;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment