在 React Router 中传递附加参数
Passing additional parameters in React Router
如何将附加参数传递到我要转换到的组件。
我的 routes.js 如下。我已经声明了两条路径,一条用于 authorList,另一条用于特定作者的详细信息。
var routes = (
<Route path="/" component={require('./components/main')}>
<Route path="authors" component={require('./components/authors/authorPage')}/>
<Route path="authors/:authorId" component={require('./components/authors/AuthorDetails')}/>
</Route>
);
module.exports = routes;
在我的authorList页面中有如下功能。
showAuthorDetails(authorId) {
var myExtraParams = { key1 : val1, key2 : val2};
hashHistory.push(`/authors/${authorId});
}
现在在我的 AuthorDetail 页面中,我可以通过
获取 authorId
this.props.params.authorId
但我也想将 myExtraParams 作为对象传递,但不想在 url 中声明和传递它。
我想以某种方式访问新组件中的 myExtraParams,也许可以说是通过
this.props.params.myExtraParams
应该给mt这个对象。 (就像它在 Angular UI 路由器中通过使用 stateParams 发生的方式一样)
我该怎么做?
您可以尝试像这样更改路由的结构:
var routes = (
<Route path="/" component={require('./components/main')}>
<Route path="authors" component={require('./components/authors/authorPage')}>
<Route path="author/:authorId" component={require('./components/authors/AuthorDetails')}/>
</Route>
</Route>
);
然后在你的 authorList 页面你可以做
...
renderAuth() {
if (this.props.children === null) {
return(
....your default authorList
)
} else {
return React.cloneElement(this.props.children || <div />, {myExtraParams: myExtraParams});
}
}
render() {
<div>
{this.renderAuth()}
</div>
}
showAuthorDetails(authorId) {
hashHistory.push(`/authors/author/${authorId});
}
...
然后您应该能够在您的作者详细信息页面
中访问 this.props.myExtraParams
我正在看这个问题。也许我来晚了,你已经找到了解决方案,但如果没有,那么你可以通过
router.push({
pathname: '/some-route',
search: '?param=123',
state: {
additionalParam: 'value',
},
})}
在接收组件端,您将得到它
this.props.location.state.additionalParam
希望对您有所帮助。如果需要任何进一步的帮助,请随时告诉我。
谢谢
如何将附加参数传递到我要转换到的组件。
我的 routes.js 如下。我已经声明了两条路径,一条用于 authorList,另一条用于特定作者的详细信息。
var routes = (
<Route path="/" component={require('./components/main')}>
<Route path="authors" component={require('./components/authors/authorPage')}/>
<Route path="authors/:authorId" component={require('./components/authors/AuthorDetails')}/>
</Route>
);
module.exports = routes;
在我的authorList页面中有如下功能。
showAuthorDetails(authorId) {
var myExtraParams = { key1 : val1, key2 : val2};
hashHistory.push(`/authors/${authorId});
}
现在在我的 AuthorDetail 页面中,我可以通过
获取 authorIdthis.props.params.authorId
但我也想将 myExtraParams 作为对象传递,但不想在 url 中声明和传递它。 我想以某种方式访问新组件中的 myExtraParams,也许可以说是通过
this.props.params.myExtraParams
应该给mt这个对象。 (就像它在 Angular UI 路由器中通过使用 stateParams 发生的方式一样)
我该怎么做?
您可以尝试像这样更改路由的结构:
var routes = (
<Route path="/" component={require('./components/main')}>
<Route path="authors" component={require('./components/authors/authorPage')}>
<Route path="author/:authorId" component={require('./components/authors/AuthorDetails')}/>
</Route>
</Route>
);
然后在你的 authorList 页面你可以做
...
renderAuth() {
if (this.props.children === null) {
return(
....your default authorList
)
} else {
return React.cloneElement(this.props.children || <div />, {myExtraParams: myExtraParams});
}
}
render() {
<div>
{this.renderAuth()}
</div>
}
showAuthorDetails(authorId) {
hashHistory.push(`/authors/author/${authorId});
}
...
然后您应该能够在您的作者详细信息页面
中访问 this.props.myExtraParams我正在看这个问题。也许我来晚了,你已经找到了解决方案,但如果没有,那么你可以通过
router.push({
pathname: '/some-route',
search: '?param=123',
state: {
additionalParam: 'value',
},
})}
在接收组件端,您将得到它
this.props.location.state.additionalParam
希望对您有所帮助。如果需要任何进一步的帮助,请随时告诉我。
谢谢