簡體   English   中英

ReactJS react-router

[英]ReactJS react-router

我正在使用ReactJS開發應用程序,但我遇到了問題。

那是我的路由器

<Router history={browserHistory}>
    <Route name="Home" path="/" component={App}>
        <IndexRoute component={Home}></IndexRoute>
        <Route name="Stores" path='stores' component={Stores}>
        <Route name="List" path='/stores/list' component={StoreList}></Route>
        <Route name="Create" path='/stores/create' component={StoreCreate}>   </Route>
    </Route>
    <Route name="404: No Match for route" path="*" component={NoMatch} />
    </Route>
</Router>

商店的路線不存在。 但是,當我點擊此路線的鏈接時,他們必須重定向做“StoreList”

<Route name="Stores" path='stores' component={Stores}>

所以我做了:

    class Stores extends React.Component {

      componentWillMount() {
        this.setUrlParameters();
      }

      setUrlParameters() {
        if (!this.props.children) {
          this.context.router.push({
              pathname: '/stores/list',
              query: {
                  page: 1,
                  limit: 10
              }
          });
        }
      }

      render() {
        return (this.props.children);
      }
    }

我第一次點擊鏈接時,他們確實完善了重定向。 但是,如果我再次單擊,它們不會調用componentWillMount ,並且重定向不會執行。

有人能幫我嗎?

嘗試在componentDidUpdate中調用this.setURlParameters()而不是componentWillMount

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM