Created
December 21, 2018 11:23
-
-
Save jamesseanwright/c5c97e9e4b513564fb89d7c5de151442 to your computer and use it in GitHub Desktop.
React Provider Example
This file contains 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
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