简体   繁体   English

将props传递给react-router 2.4中的路由

[英]passing props to routes in react-router 2.4

I would like to pass in some props from the router to the components 我想从路由器传递一些道具到组件

Something like this: 像这样的东西:

<Route component={Foo} some_prop={12}/>

Is there any way to do this with the latest react-router? 有没有办法用最新的react-router做到这一点? I know it was not possible (without using a wrapper) pre 1.0. 我知道不可能(不使用包装器)pre 1.0。

If you're passing them directly to the <Route /> component like that, it's now possible to do it exactly the way you're suggesting: 如果您将它们直接传递给<Route />组件,现在可以按照您建议的方式完成:

<Route foo="bar" />

Will give you access to the foo prop on this.props.route.foo 将允许您访问this.props.route.foo上的foo prop

Via: https://github.com/reactjs/react-router/blob/master/docs/Troubleshooting.md#passing-additional-values-into-route-components 通过: https//github.com/reactjs/react-router/blob/master/docs/Troubleshooting.md#passing-additional-values-into-route-components

As a side note: if you're using something higher level like <RouterContext /> it is still necessary to use a data wrapper component of some kind, unfortunately. 作为旁注:如果你正在使用更高级别的东西,如<RouterContext /> ,遗憾的是仍然需要使用某种类型的数据包装器组件。

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

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