A very simple App. At the / path I want to render Home when logged in and Introduction if not signed in. Logging in on the Login page works, if i console.log(firebase.auth().currentUser) it shows all the data and on / it renders Home correctly. So, while still being at the / path, something weird happens; when i refresh page we are not logged in anymore and it renders Introduction. But when I click on the link <li><Link to='/'>Home</Link></li> the Home component renders correctly and we are logged in again. Why is this happening?
import React, { Component } from 'react';
import {BrowserRouter as Router, Route, Switch, Link, Redirect} from 'react-router-dom';
import firebase from './firebase'
import Home from './containers/home'
import Login from './containers/login'
import Register from './containers/register'
import Dashboard from './containers/dashboard'
const Protected = ({component: Component, ...rest}) => (
<Route {...rest} render={(props) => {
if(firebase.auth().currentUser)
return <Component {...props} />
else
return <Redirect to='/register' />
}} />
)
const Introduction = props => ( <h1>hello</h1>)
class App extends Component {
render() {
return (
<Router>
<React.Fragment>
<ul>
<li><Link to='/'>Home</Link></li>
<li><Link to='/register'>Register</Link></li>
<li><Link to='/login'>login</Link></li>
<li><Link to='/dashboard'>dashboard</Link></li>
</ul>
<Switch>
<Route path='/' exact render={(props) => {
if(firebase.auth().currentUser)
return <Home {...props} />
else
return <Introduction {...props} />
}
} />
<Route path='/register' exact component={Register} />
<Route path='/login' component={Login} />
<Protected path='/dashboard' component={Dashboard} />
</Switch>
</React.Fragment>
</Router>
);
}
}
export default App;