Skip to content

Instantly share code, notes, and snippets.

@maecapozzi
Last active August 16, 2018 17:42
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 maecapozzi/759a6138f2f434588922dc36ae4b52ab to your computer and use it in GitHub Desktop.
Save maecapozzi/759a6138f2f434588922dc36ae4b52ab to your computer and use it in GitHub Desktop.
import React from "react";
import { FamilyProvider, FamilyConsumer } from "./FamilyContext";
export class Grandmother extends React.Component {
state = {
lastName: "Sanchez"
};
render() {
return (
// We wrap all of the components that need access
// to the lastName property in FamilyProvider.
<FamilyProvider value={this.state.lastName}>
<Mother />
</FamilyProvider>
);
}
}
const Mother = () => {
return <Child />;
};
const Child = () => {
// We wrap the component that actaully needs access to
// the lastName property in FamilyConsumer
return <FamilyConsumer>{context => <p>{context}</p>}</FamilyConsumer>;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment