Reactjs 为什么手动键入React Routes无效,它会重定向到另一个页面

Reactjs 为什么手动键入React Routes无效,它会重定向到另一个页面,reactjs,redux,react-redux,react-router,Reactjs,Redux,React Redux,React Router,这是我的App.js类。它连接到Redux商店 class App extends Component { componentDidMount() { this.props.checkLocalStorageIfTokenExists(); } render() { let routes = null; if (this.props.isAuthenticated) { routes = ( <Switch>

这是我的App.js类。它连接到Redux商店

class App extends Component {
  componentDidMount() {
    this.props.checkLocalStorageIfTokenExists();
  }

  render() {
    let routes = null;
    if (this.props.isAuthenticated) {
      routes = (
        <Switch>
          <Route path="/dashboard" component={Dashboard} />
          <Route path="/settings" component={Settings} />
          <Route path="/details/:countryId" component={CountryDetails} />
          <Redirect to="/dashboard" />
        </Switch>
      );
    } else {
      routes = (
        <Switch>
          <Route path="/login" exact component={Login} />
          <Route path="/register" exact component={Register} />
          <Route path="/" exact component={Home} />
          <Redirect to="/" />
        </Switch>
      );
    }
    return routes;
  }
}

const mapStateToProps = state => {
  return {
    isAuthenticated: state.authReducer.idToken !== null
  };
};

const mapDispathToProps = dispatch => {
  return {
    checkLocalStorageIfTokenExists: () => dispatch(checkLocalStorage())
  };
};

export default withRouter(connect(mapStateToProps, mapDispathToProps)(App));
类应用程序扩展组件{
componentDidMount(){
this.props.checkLocalStorageIfTokenExists();
}
render(){
让routes=null;
如果(此.props.isAuthenticated){
路线=(
);
}否则{
路线=(
);
}
返回路线;
}
}
常量mapStateToProps=状态=>{
返回{
isAuthenticated:state.authReducer.idToken!==null
};
};
const mapDispathToProps=调度=>{
返回{
checkLocalStorageIfTokenExists:()=>调度(checkLocalStorage())
};
};
使用路由器导出默认值(connect(mapStateToProps,mapDispathToProps)(应用程序));
当我登录时,它会将我重定向到“/dashboard”,这非常完美,所有其他路由都按预期工作。但当我在浏览器中手动键入URL(例如:)时,它会将我重定向到“/仪表板”。我知道,当我手动键入URL时,整个应用程序会再次下载,如果我没有弄错,Redux商店也会再次构建

注意:checkLocalStorageIfTokenExists()方法仅检查localStorage并更改Redux LoggedIn状态(初始化令牌)


所以我的问题是。。。有什么方法可以手动键入URL并转到该页面吗?

我尝试过为相同的内容创建一个沙盒,它似乎工作得非常好,在手动输入路由时,它会呈现特定的组件。谢谢您的帮助,但是如果您添加了身份验证,那么您将理解我上面的意思。比方说,如果您是授权的,那么您不能手动进入/设置路线。如果授权为false,您只需返回null。尝试将其分为两个条件(真、假),并将两个条件的路由分开。我尝试为相同的条件创建一个沙盒,它似乎工作得非常好,在手动输入路由时,它会呈现特定的组件。谢谢您的帮助,但是如果您添加了身份验证,那么您将理解我上面的意思。比方说,如果您是授权的,那么您不能手动进入/设置路线。如果授权为false,您只需返回null。尝试将其分为两个条件(真、假),并将这两个条件的路由分开。