Last active
February 24, 2019 11:01
-
-
Save Harshmakadia/ca52fc539bcb425686b3a00cf18aa002 to your computer and use it in GitHub Desktop.
ContextType
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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