Skip to content

Instantly share code, notes, and snippets.

@emersxw
Last active June 28, 2020 18:30
Show Gist options
  • Save emersxw/afce429d9ae883095fd200e6035101ee to your computer and use it in GitHub Desktop.
Save emersxw/afce429d9ae883095fd200e6035101ee to your computer and use it in GitHub Desktop.
// 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