【问题标题】:Redirect if route is matched but there is no data for that id value如果路由匹配,则重定向,但该 id 值没有数据
【发布时间】:2019-01-28 10:54:37
【问题描述】:

这是项目路线:

<Route exact path="/party/:partyId/" component={PartyContainer} />

数据收集在Party中的componentDidMount中,

fetchWishlist(currentId);

Party 通过 mapActionToProps 与 party.Container 中的函数连接。

如果没有特定ID的数据,如何将用户重定向到另一个页面?

提前谢谢大家:)。

【问题讨论】:

    标签: reactjs redirect react-router react-redux


    【解决方案1】:

    你可以使用

    this.props.history.push('path')

    【讨论】:

      【解决方案2】:

      在PartyContainer组件中,你可以检查特定ID的数据是否存在

      return <Redirect to="/404" push={false} />
      

      this.props.history.push("/404")
      

      【讨论】:

        【解决方案3】:

        您可以将您的路线包装在 Switch 语句中:

        <Switch>
            <Route exact path= "/party/:partyId/" component = {PartyContainer}/>
            <Route exact path="/party/:partyId/partyDetails" component={PartyDetails} />
            <Route component={() => <div>Nothing found</div>} />
        </Switch>
        

        【讨论】:

        • `
          Nothing found
          } />
          ` - 这是包装,但重点是,他输入 :partyId,他接受它并传递给函数 fetchWishlist(:partyId),该函数返回数据或不返回数据。最初我的对象是空的,所以它保持空,我得到空白页。如果没有结果,我需要另一个页面。
        • 你能告诉我如何在灰色块中格式化评论吗? @CataJohn
        • @BojanIštvančić,你可以像这样使用反引号(``)`test`注意不要在反引号旁边添加任何空格
        • 我还是有这个问题。所以基本上我从一开始就有 Switch 包装器,但关键是,如果你输入 :/partyId/ 然后你然后如果你没有找到任何东西,那么重定向应该发生。它不是这样发生的,我得到 {partyContainer} 页面,但它没有数据。提前致谢!
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-10
        • 2019-05-05
        相关资源
        最近更新 更多