简体   繁体   English

迁移到react-router v4-带子级的路由,需要路由道具

[英]Migrating to react-router v4 - Route with children which requires router props

Can I get some advice on how to use my AppMain component which uses props passed down from react-router to get the data. 我可以获取有关如何使用AppMain组件的一些建议AppMain ,该组件使用从react-router传递下来的props来获取数据。

Here is what I have changed so far 到目前为止,这是我已更改的内容

v3: v3:

<Router history={browserHistory} >
  <Route component={AppMain} exact path="/" >
    <IndexRoute name="Home" component={Home} />
    <Route name="Site Details" path="/details" component={SiteDetails} />
    <Route name="Home" component={Home} path="*" />
  </Route>
</Router>

v4 v4

  <Router>
        <div>
          {/*<AppMain>*/}
          <Route component={AppMain} >
            <main>
              <Grid fluid className={classnames('app-content', { 'expanded': !this.state.mobileView && this.state.open })}>
                <Switch>
                  <Route name="Home" exact path="/" component={Home} />
                  <Route name="Site Details" exact path="/details" component={SiteDetails} />
                </Switch>
              </Grid>
            </main>
          </Route>
          {/*</AppMain>*/}
        </div>
  </Router>

As you can see, I have attempted multiple methods of showing AppMain and Home / SiteDetails at the same time. 如您所见,我尝试了同时显示AppMainHome / SiteDetails多种方法。 This method gives me this warning which shows AppMain but none of its children: 此方法给我以下警告,该警告显示AppMain但不显示AppMain

You should not use <Route component> and <Route children> in the same route; <Route children> will be ignored

The other method of using <AppMain> doesn't pass the props.location.state... which I am trying to convert my AppMain code to here: 使用<AppMain>的另一种方法没有传递props.location.state... ,我正在尝试将AppMain代码转换为此处:

v3 v3

if (props.params.division !== undefined) {
  this.getDivision(props.params.division);
} else {
  this.getDivisions();
}

v4 v4

if (props.location.state !== undefined) {
  if (props.location.state.division !== undefined) {
    this.getDivision(props.location.state.division.code);
  } else {
    this.getDivisions();
  }
} else {
  this.getDivisions();
}

What if: 如果:

React Router v4: React Router v4:

     <Router>
        <div>
          <AppMain>
            <main>
              <Grid fluid className={classnames('app-content', { 'expanded': !this.state.mobileView && this.state.open })}>
                <Switch>
                  <Route name="Home" exact path="/" component={Home} />
                  <Route name="Site Details" exact path="/details" component={SiteDetails} />
                </Switch>
              </Grid>
            </main>
          </AppMain>
        </div>
     </Router>

If you need location props in your AppMain , just wrap it with withRouter component: https://github.com/ReactTraining/react-router/blob/master/packages/react-router/docs/api/withRouter.md 如果您在AppMain需要定位道具,只需将其与withRouter组件一起包装withRouterhttps : //github.com/ReactTraining/react-router/blob/master/packages/react-router/docs/api/withRouter.md

Turns out all I needed to do was add a Route to AppMain outside the Switch with no path. 事实证明,我要做的就是在Switch外部添加到AppMainRoute ,而没有路径。

Here is my fix: 这是我的解决方法:

  <Router>
        <div>
          <Route component={AppMain} />
            <main>
              <Grid fluid className={classnames('app-content', { 'expanded': !this.state.mobileView && this.state.open })}>
                <Switch>
                  <Route name="Site Details" exact path="/details" component={SiteDetails} />
                  <Route name="Home" component={Home} />
                </Switch>
              </Grid>
            </main>
        </div>
  </Router>

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

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