2015-10-16 72 views
3

react-router版本0.13.3我穿过状态反应路由器的1.x

Router.run(routes, createBrowserHistory, function(Handler, state) { 
    React.render(<Handler query={ state } path={ state.pathname }/>, document.getElementById('app')) 
}); 

react-router 1.0.0.rc3我

let history = createBrowserHistory(); 
React.render(<Router history={ history } >{ routes }</Router>, document.getElementById('app')); 

随着react-router 0.13.3,我能够通过回调将状态传递给我的组件。我想知道我可以如何做react-router 1.x?

回答

3

阵营路由器1.0现在从您的路径组件创建的元素,将它们下面的路由相关的状态道具默认:

  • history - 一个History对象
  • location - 一个Location对象
  • params - 从URL提取的参数
  • route - 渲染组件所属的路由对象
  • routeParams - 从URL的路径提取参数对象的渲染组件属于
  • routes - 所有的路由对象,其相匹配

您应该能够使用这些得到的数组保持与路由有关的状态,例如要获取路径名,您可以在路由组件中使用this.props.location.pathname,或者将这些传递给需要它们的子节点。

The 1.0 upgrade guide has more details about this


如果你想渲染时传递额外的道具到各个路由组件,您可以使用React.cloneElement()像这样:

{React.cloneElement(this.props.children, {extra: 'extra prop'}) 

如果您想在路由传递其他道具组件元素正在创建中,您可以pass a custom createElement() function to <Router>