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

Mit*_*uyn 3 reactjs

我想从路由器传递一些道具到组件

像这样的东西:

<Route component={Foo} some_prop={12}/>
Run Code Online (Sandbox Code Playgroud)

有没有办法用最新的react-router做到这一点?我知道不可能(不使用包装器)pre 1.0.

Bra*_*rst 6

如果您将它们直接传递给这样的<Route />组件,现在可以按照您建议的方式完成:

<Route foo="bar" />
Run Code Online (Sandbox Code Playgroud)

会让你访问foo道具this.props.route.foo

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

作为旁注:如果您正在使用更高级别的东西,<RouterContext />那么仍然需要使用某种类型的数据包装器组件,不幸的是.