Skip to content

Instantly share code, notes, and snippets.

@jamesseanwright
Created December 21, 2018 11:23
Show Gist options
  • Save jamesseanwright/c5c97e9e4b513564fb89d7c5de151442 to your computer and use it in GitHub Desktop.
Save jamesseanwright/c5c97e9e4b513564fb89d7c5de151442 to your computer and use it in GitHub Desktop.
React Provider Example
const UserContext = React.createContext(maybe<User>());
const App: React.FC<AppProps> = ({ user }) => (
<UserContext.Provider value={maybe(user)}>
<Header />
</UserContext.Provider>
);
const Header = () => (
<>
<Logo />
<Account />
</>
);
const Account = () => (
<UserContext.Consumer>
{user =>
user.isNothing()
? <SignInButton />
: <p>Signed in as {user.value().name}</p>
}
</UserContext.Consumer>
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment