Last active
October 29, 2019 13:20
-
-
Save buglessir/f057a53ea4760500264c979426163a63 to your computer and use it in GitHub Desktop.
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
import React from 'react'; | |
import ReactDOM from 'react-dom'; | |
import { BrowserRouter, Switch, Route, Link, Redirect } from 'react-router-dom'; | |
function Auth() { | |
return false; | |
} | |
const Admin = () => ( | |
<h1>This is Admin page</h1> | |
) | |
const Login = () => ( | |
<h1>Login Page</h1> | |
) | |
const Home = () => ( | |
<h1>Home Page</h1> | |
) | |
const ProtectedRoute = ({ component: Component, ...rest }) => ( | |
<Route {...rest} render={(props) => ( | |
Auth() === true | |
? <Component {...props} /> | |
: <Redirect to='/login' /> | |
)} /> | |
) | |
const App = () => ( | |
<> | |
<ul> | |
<li> | |
<Link to="/">Home</Link> | |
</li> | |
<li> | |
<Link to="/admin">Admin</Link> | |
</li> | |
</ul> | |
<Switch> | |
<Route exact path="/" component={Home} /> | |
<Route path="/login" component={Login} /> | |
<ProtectedRoute path="/admin" component={Admin} /> | |
</Switch> | |
</> | |
) | |
ReactDOM.render( | |
<BrowserRouter> | |
<App /> | |
</BrowserRouter>, | |
document.getElementById('root') | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment