Skip to content

Instantly share code, notes, and snippets.

@Harshmakadia
Last active February 24, 2019 11:01
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/ca52fc539bcb425686b3a00cf18aa002 to your computer and use it in GitHub Desktop.
Save Harshmakadia/ca52fc539bcb425686b3a00cf18aa002 to your computer and use it in GitHub Desktop.
ContextType
import ShopContext from '../context/shop-context'
class CartPage extends Component {
static contextType = ShopContext
componentDidMount() {
// Some advantage of static contextType: We can now also access Context in the rest of the component
console.log(this.context)
}
render() {
return (
<React.Fragment>
<Navigation
cartItemNumber={this.context.cart.reduce((count, curItem) => {
return count + curItem.quantity
}, 0)}
/>
<main className="cart">...</main>
</React.Fragment>
)
}
}
export default CartPage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment