Created
November 5, 2017 20:16
-
-
Save Southclaws/839cb086a54f2cbbedb88556195c8ed3 to your computer and use it in GitHub Desktop.
I wrestled with this for a while so I figured I'd share it.
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
class App extends React.Component<{}, AppState> { | |
// IfLoggedIn is a TypeScript version of: | |
// https://reacttraining.com/react-router/web/example/auth-workflow | |
// it's just a stateless component that takes path and component (similar to <Route>) | |
// and renders the component if the user is logged in, if not, it redirects to /login | |
IfLoggedIn = (thisProps: { path: string; component: Function }) => { | |
return ( | |
<Route | |
render={props => | |
this.state.loggedIn ? ( | |
thisProps.component | |
) : ( | |
<Redirect | |
to={{ | |
pathname: "/login", | |
state: { from: props.location } | |
}} | |
/> | |
)} | |
/> | |
); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment