2015-10-16 71 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>