自动从“ /”路径重定向到“ / login”或“ / admin”

时间:2019-05-04 21:09:35

标签: reactjs redux react-router

当应用程序从“ localhost:3000 /”启动时,我需要进行自动重定向。这意味着自动从“ /”重定向到“ / login”或“ / admin”。

我试图做到这一点:

imports ...... 

class App extends react component {

state = {isAuth:false}


func = () => {
//axios function that goes to node.js to pass json-web-token and receive payload.
if success: this setState({isAuth:true})

}

render () {

if (!isAuth) return <Redirect to="/login">

if (isAuth) return <Redirect to="/admin">


return (
      <Router>
       {this.func()}
        <div className="App">
          <div className="container">
            <Route exact path="/addNewVacation" component= 
              {VacationAdder} />
            <Route path="/admin" component={Admin} />
            <Route path="/home" component={Home} />
            <Route exact path="/register" component={Register} />
            <Route exact path="/login" component={Login} />
          </div>
        </div>
      </Router>
    );


}


}

1 个答案:

答案 0 :(得分:0)

对axios的调用是异步的,因此调用axios时需要等待,并声明

相关问题