Skip to content

Instantly share code, notes, and snippets.

@alexandrzavalii
Last active March 26, 2019 22:57
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 alexandrzavalii/b3cc2efa243e1cff2d5fc53f3f73bc56 to your computer and use it in GitHub Desktop.
Save alexandrzavalii/b3cc2efa243e1cff2d5fc53f3f73bc56 to your computer and use it in GitHub Desktop.
/* HOC for wrapping the component with Context Provider */
const connect = Component => {
return function WrappedComponent(props) {
return (
<CandyContext.Consumer> /* pass lollipops as a prop */
{value => <Component {...props} lollipops={value} />}
</CandyContext.Consumer>
);
};
};
/* ChildComponent.js */
const FirstChild = props => (
<p>
I am John, and I am wearing {props.clothing} today.
{props.lollipops ? "thanks for lollipop" : "I want lolipop"}
</p>
);
/* subscribe FirstChild to Context Changes */
export default connect(FirstChild);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment