Last active
December 7, 2017 02:44
-
-
Save saaage/600ffaba122cc0b56d0c5eda0f2e25f7 to your computer and use it in GitHub Desktop.
Conditional Routes
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, { Component } from 'react' | |
import { Switch, Route } from 'react-router-dom' | |
import PrivateRoute from 'components/PrivateRoute' | |
import Login from 'components/Login' | |
class App extends Component { | |
state = {} | |
render() { | |
const { isAuthenticated } = this.props | |
return ( | |
<div> | |
{/* PUBLIC */} | |
{!isAuthenticated && | |
<span> | |
<h1>user not logged in, render Login component:</h1> | |
<Login /> | |
</span> | |
// <Switch> | |
// <Route path={'/'} exact component={Home} /> | |
// <Route path={'/login'} component={Login} /> | |
// <Route path={'/register'} component={Register} /> | |
// <Route path={'/forgot'} component={Forgot} /> | |
// <Route path={'/verify'} component={Verify} /> | |
// </Switch> | |
} | |
{/* PRIVATE */} | |
{isAuthenticated && | |
<span> | |
<h2>user is logged in, render dashboard:</h2> | |
<PrivateRoute isAuthenticated={isAuthenticated} user="Billy" /> | |
</span> | |
} | |
</div> | |
) | |
} | |
} | |
export default App |
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 ReactDOM from 'react-dom' | |
import { BrowserRouter as Router } from 'react-router-dom' | |
import App from 'components/App' //will be a different location depending on your folder structure | |
ReactDOM.render( | |
<Router> | |
<App isAuthenticated /> | |
// I am mocking isAuthenticated here, your isAuthenticated prop will be provided by connect | |
</Router>, | |
document.getElementById('root') | |
) |
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' | |
const Login = () => ( | |
<form> | |
<input placeholder="username" /> | |
<input placeholder="password" /> | |
</form> | |
) | |
export default Login |
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' | |
const PrivateRoute = props => ( | |
<div> | |
<p>Hi from dashboard</p> | |
<p>According to props isAuthenticated is {props.isAuthenticated.toString()}</p> | |
<p>User name is {props.user} </p> | |
</div> | |
) | |
export default PrivateRoute |
I just found what I had wrong, it was a matter of syntax: props.isAuthenticated should have been rest.isAuthenticated!!
Add the PrivateRoute component to your answer so I can give the answer to you.
Thank you so much for your help!
Awesome man! Glad you were able to get it done. Would you mind marking the answer correct on Stack when you get a chance. Thanks.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, this is how I defined my PrivateRoute:
And App.js: