[英]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. 如您所见,我尝试了同时显示
AppMain
和Home
/ 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
组件一起包装withRouter
: https : //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
外部添加到AppMain
的Route
,而没有路径。
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.