简体   繁体   English

react-router-dom 重定向不重定向到我想要的组件,但它正在重定向到不同的路径

[英]react-router-dom redirect not redirecting to the component I want it to but it is redirecting to a different path

Versions版本

"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-redux": "^7.2.0",
"react-router-dom": "^5.2.0",

I have an issue where my conditional Redirect component is not working as it should.我有一个问题,即我的条件Redirect组件无法正常工作。 Is it because it's outside of the Switch component.是不是因为它在Switch组件之外。 I want it to redirect to the /error route if apiError is true.如果 apiError 为真,我希望它重定向到/error路由。 The conditional works since I tested with just the Error component and not the error route.条件有效,因为我仅使用Error组件而不是错误路由进行了测试。 The Redirect component also works since the path does change but the component is not rendered. Redirect组件也可以工作,因为路径确实发生了变化,但组件没有被渲染。

render() {
    const { currentUser, isLoaded, apiError } = this.state;
    return ( 
      <Router>
        {currentUser ? (
          <div className='App'>
            <Switch>
              <Route exact path='/title/:id' render={props => <ShowTitle {...props} /> } />
              <Route exact path='/shuffle' render={props => <Titles {...props} isLoaded={isLoaded} /> } />
              <Route exact path='/about' render={props => <About {...props} /> } />
              <Route exact path='/all-titles' render={props => <Titles {...props} isLoaded={isLoaded} /> } />
              <Route exact path='/error' render={props => <Error {...props} />} />
              <Route exact path='/' render={props => <Redirect to={'/' + currentUser.viewType} /> } />
              <Route exact path='/logout' component={Logout} />
              <Redirect to='/all-titles' />
            </Switch>
          </div>
        ) : apiError ? <Redirect to='/error' /> : (
          <Landing isLoaded={this.state.isLoaded}></Landing>
        )}
      </Router>
    );

I'm seeing a logical error with it.我看到了一个逻辑错误。

<Route exact path='/error' render={props => <Error {...props} />} />

Doesn't exist when apiError . apiError时不存在。

This probably work for you:这可能对你有用:

render() {
    const { currentUser, isLoaded, apiError } = this.state;
    let route = (<Landing isLoaded={isLoaded} />);
    if (currentUser) {
      route = (
        <div className='App'>
          <Switch>
            <Route exact path='/title/:id' render={props => <ShowTitle {...props} /> } />
            <Route exact path='/shuffle' render={props => <Titles {...props} isLoaded={isLoaded} /> } />
            <Route exact path='/about' render={props => <About {...props} /> } />
            <Route exact path='/all-titles' render={props => <Titles {...props} isLoaded={isLoaded} /> } />
            <Route exact path='/error' render={props => <Error {...props} />} />
            <Route exact path='/' render={props => <Redirect to={'/' + currentUser.viewType} /> } />
            <Route exact path='/logout' component={Logout} />
            <Redirect to='/all-titles' />
          </Switch>
        </div>
      );
    }
    if (apiError) {
      route = (
        <Switch>
          <Route exact path='/error' render={props => <Error {...props} />} />
        </Switch>
      );
    }
    return ( 
      <Router>
        {route}
      </Router>
    );
};

For better usage, I recommend to you declare all routes statically, on HOC module, then validate props and use Redirect component to navigate to other path.为了更好地使用,我建议您在 HOC 模块上静态声明所有路由,然后验证 props 并使用Redirect组件导航到其他路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM