Created
November 3, 2019 13:07
-
-
Save pubudu-ranasinghe/fb9e5996bc948b21a0e6e004f800983c to your computer and use it in GitHub Desktop.
react-context-example-20
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 React from "react"; | |
import { Route, Redirect } from "react-router-dom"; | |
import { useAuthContext } from "../contexts/AuthContext"; | |
export default function PrivateRoute({ children, ...rest }) { | |
const { auth } = useAuthContext(); | |
return ( | |
<Route | |
{...rest} | |
render={({ location }) => | |
auth.isLoggedIn ? ( | |
children | |
) : ( | |
<Redirect to={{ pathname: "/", state: { from: location } }} /> | |
) | |
} | |
/> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment