Created
August 28, 2018 15:19
-
-
Save drex44/56bb0fe08b836e30c20572b73e596619 to your computer and use it in GitHub Desktop.
Create private/protected route in react using react router v4
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
import { Switch, Route, Redirect } from "react-router-dom"; | |
export const PrivateRoute = ({ component: Component, ...rest }) => ( | |
<Route | |
{...rest} | |
render={props => | |
props.isLoggedIn === true ? ( | |
<Component {...props} /> | |
) : ( | |
<Redirect | |
to={{ | |
pathname: "/", | |
state: { from: props.location } | |
}} | |
/> | |
) | |
} | |
/> | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to use this component,