Created
January 15, 2020 13:58
-
-
Save mcavaliere/7fe8e8f20391fc4c50ffac618a215dcc to your computer and use it in GitHub Desktop.
React Context API Logged-in content helpers
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
export const LoggedInContent: FC<{}> = props => ( | |
<AuthContext.Consumer> | |
{(auth): React.ReactNode => (auth && auth.token ? props.children : null)} | |
</AuthContext.Consumer> | |
); | |
/** | |
* Helper to show content only if the user is logged out. | |
*/ | |
export const LoggedOutContent: FC<{}> = props => ( | |
<AuthContext.Consumer> | |
{(auth): React.ReactNode => (!auth || !auth.token ? props.children : null)} | |
</AuthContext.Consumer> | |
); |
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
<LoggedInContent> | |
<Text>LOGGED IN!</Text> | |
</LoggedInContent> | |
<LoggedOutContent> | |
<Text>NOT LOGGED IN!</Text> | |
</LoggedOutContent> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment