I'm trying to build a login form using react and node.js and the Redirect & Routing doesn't work for me.
I have set in the App.js the Routes, and called a component which validate's all the variables and needs to redirect to Admin page or User page. My problem is with the redirect. If im redirecting I'm reciving the "Maximum update depth exceeded" error, and if im routing im the page is blank.
//App.js:
<BrowserRouter>
<div className="App">
<CheckLogin/> {/*Validation component */}
<Route exact path="/" component={Login}/>
<Route path="/admin" component={AdminHome}/>
<Route path="/user" component={UserHome}/>
</div>
</BrowserRouter>
//CheckLogin.js:
state={
UserDataFromDB:{},
}
componentDidMount=()=>
{
if (this.state.UserDataFromDB.ans)
{
console.log('there is ans');
}
else
{
axios.get(`http://localhost:4000/CheckLogin`, { withCredentials: true })
.then(response => {
// this.setState({ Cookie: response.data.id });
this.setState({ UserDataFromDB: response.data })
})
.catch(error => {
console.log("error: ", error);
})
}
}
RouteSomewhere()
{
if(this.state.UserDataFromDB.ans==0)
{
return <Redirect to='/'/>
}
if (this.state.UserDataFromDB.role==1)
{
return<Redirect to="/admin"/>
}
if (this.state.UserDataFromDB.role==0)
{
return <Redirect to="/user"/>
}
}
render() {
return (
<div className="CheckLogin">
{this.RouteSomewhere()}
</div>
);
}
}
I expect that the page will Redirect into the other page.. now im reciving the "Maximum update depth exceeded" Error.
Thanks :)