[英]How do you access url args in react-router-dom 4 when using Route render prop instead of Route component prop?
I am using react-router-dom v 4.0.0. 我正在使用react-router-dom v 4.0.0。
In my top-level <App />
component that renders my react router, I have three routes. 在渲染反应路由器的顶级
<App />
组件中,我有3条路由。 The first two work as expected, the third one does not. 前两个按预期工作,第三个则没有。
render() {
const pageMain = (
<PageMain token={this.state.token} />
);
const pageActivity = (
<PageActivity
projectActivities={this.state.projectActivities}
projectActivitiesMap={this.state.projectActivitiesMap} />
);
return (<Router>
<div>
<Route
path="/"
component={PageSplash}
/>
<Route
path="/app"
render={props => pageMain}
/>
<Route
path="/activity/:activityId/"
render={props => pageActivity}
/>
</div>
</Router>);
}
When I go to www.example.com/ , the router directs me to my PageSplash component as expected. 当我转到www.example.com/时 ,路由器会按预期将我定向到我的PageSplash组件。
When I go to www.example.com/app , the router directs me to my PageMain component as expected. 当我转到www.example.com/app时 ,路由器会按预期将我定向到我的PageMain组件。 Further, in my PageMain component, I have access to
this.props.token
as expected. 此外,在我的PageMain组件中,我可以按预期访问
this.props.token
。
When I go to www.example.com/activity/12345/ , the router directs me to my PageActivity component as expected. 当我转到www.example.com/activity/12345/时 ,路由器会按预期将我定向到我的PageActivity组件。 In my PageActivity component, I have access to
this.props.projectActivities
and this.props.projectActivitiesMap
as expected. 在我的PageActivity组件中,可以按预期访问
this.props.projectActivities
和this.props.projectActivitiesMap
。 However, I'd like to be able to access the activityId from the url, which should be 12345 in this case. 但是,我希望能够从URL(在这种情况下应为12345)访问activityId。 In PageActivity, I don't receive
this.props.params
as I thought, from reading countless pages of documentation. 在PageActivity中,我从阅读无数文档页面时并未收到我想的
this.props.params
。
I started by using simple routes like my first one. 我从使用像我的第一个这样的简单路线开始。 Then I needed to pass data from my top-level component to the /app route, so I begin using the
<Route />
render
prop instead of the component
prop. 然后,我需要将数据从顶级组件传递到/ app路由,因此我开始使用
<Route />
render
道具而不是component
道具。 This works fine. 这很好。 Then I needed to pass data and access the url param.
然后,我需要传递数据并访问url参数。 I can access the passed props, but my PageActivity component isn't receiving a params prop.
我可以访问传递的道具,但是我的PageActivity组件没有收到参数道具。 How do I access the activityId in my PageActivity component?
如何访问我的PageActivity组件中的activityId?
I figured out how to do this. 我想出了怎么做。 I needed to inline my PageActivity component, and add
{...props}
. 我需要内联PageActivity组件,并添加
{...props}
。 The problematic route looks like this now: 有问题的路线现在看起来像这样:
<Route path="/activity/:activityId/" render={props => (
<PageActivity
projectActivities={this.state.projectActivities}
projectActivitiesMap={this.state.projectActivitiesMap}
{...props}
/>
)} />
Also, in react-router-dom 4.0.0, page params are passed in this.props.match.params
. 同样,在react-router-dom 4.0.0中,页面参数在
this.props.match.params
中传递。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.