Skip to content

Instantly share code, notes, and snippets.

@Harshmakadia
Last active February 24, 2019 10:55
Show Gist options
  • Save Harshmakadia/8f115a7c3e5899ff83acf4fcac85ea9e to your computer and use it in GitHub Desktop.
Save Harshmakadia/8f115a7c3e5899ff83acf4fcac85ea9e to your computer and use it in GitHub Desktop.
Provide 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: []
};
render() {
return (
<ShopContext.Provider
value={{
products: this.state.products,
cart: this.state.cart
}}
>
{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