Skip to content

Instantly share code, notes, and snippets.

@mcavaliere
Created January 15, 2020 13:58
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 mcavaliere/7fe8e8f20391fc4c50ffac618a215dcc to your computer and use it in GitHub Desktop.
Save mcavaliere/7fe8e8f20391fc4c50ffac618a215dcc to your computer and use it in GitHub Desktop.
React Context API Logged-in content helpers
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>
);
<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