Last active
June 28, 2020 18:30
-
-
Save emersxw/afce429d9ae883095fd200e6035101ee to your computer and use it in GitHub Desktop.
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
// Example of a Private Route | |
import React from 'React' | |
import { Route as ReactDOMROute, RouteProps, Redirect } from 'react-router-dom' | |
import { useAuth } from 'useAuth' | |
function Route({ isPrivate: false, component: Component, ...rest }) { | |
const { user } = useAuth(); | |
return ( | |
<ReactDOMROute | |
{...rest} | |
render={({ location }) => { | |
return isPrivate === !!user ? ( | |
<Component /> | |
) : ( | |
<Redirect | |
to={{ | |
pathName: isPrivate ? '/login' : '/dashboard', | |
state: { from: location} | |
}} | |
/> | |
) | |
}} | |
/> | |
) | |
} | |
export default Route | |
// <Route path="login" component={Login} /> | |
// <Route path="/dashboard" component={Dashboard} isPrivate /> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment