Hi I am pretty new to ReactJS, I am trying to move to the login page when Logout is clicked, the function is getting executed but it wont redirect to the login page. I don't know how to do it. I have tried using this.props.history.push
but that is also not working. I have also tried using Redirect to="/login" in the fakeAuth.signout
function.
import React, { Component } from 'react';
import './App.css';
import GetTodo from './component/getTodo.js';
import AddTodo from './component/addTodo.js';
import Login from './component/login.js';
import {BrowserRouter as Router,Switch,Route,Link,Redirect} from
'react-router-dom';
export const fakeAuth = {
isAuthenticated: false,
authenticate(cb) {
this.isAuthenticated = true;
},
signout(cb) {
this.isAuthenticated = false;
}
}
const PrivateRoute = ({ component: Component, ...rest }) => (
<Route {...rest} render={(props) => (
fakeAuth.isAuthenticated === true
? <Component {...props} />
: <Redirect to='/login' />
)} />
)
class App extends Component {
constructor(props){
super(props);
this.newsignout=this.newsignout.bind(this);
}
newsignout(){
fakeAuth.signout();
localStorage.clear();
<Redirect to="/login" />
}
render() {
return (
<Router>
<div className='App'>
<nav className="navbar navbar-expand-lg navbar-light bg-light">
<div className="collapse navbar-collapse">
<div className="navbar-nav">
<div className="nav-item nav-link"><Link to={'/'}>GET TODO LIST</Link></div>
<div className="nav-item nav-link"><Link to={'/add'}>ADD TODO</Link></div>
<div className="nav-item nav-link"><Link to={'/login'}>LOGIN</Link></div>
<div className="nav-item nav-link "><div className="LogOut" onClick={()=>this.newsignout()}>LOGOUT</div></div>
</div>
</div>
</nav>
<Switch>
<PrivateRoute exact path='/' component={GetTodo} />
<PrivateRoute path='/add/:id' component={AddTodo} />
<PrivateRoute exact path='/add' component={AddTodo} />
<Route path='/login' component={Login}/>
</Switch>
</div>
</Router>
);
}
}
export default App;