2016-03-23 57 views
1

我有一個有趣的難題,我想深入瞭解一下。我有一個路由設置如下:在父組件中訪問URL參數

<Router history={hashHistory}> 
    <Route path='/' component={Main}> 
    <IndexRoute component={Home} /> 
    <Route path='foo' component={FooContainer}> 
     <Route path='bars/:barId' component={BarDetailsContainer} /> 
     <Route path='baz/:bazId' component={BazDetailsContainer} /> 
    </Route> 
    <Route path='things' component={ThingListContainer}> 
     <Route path='thing/:thingId' component={ThingDetailsContainer} /> 
    </Route> 
    </Route> 
</Router> 

它的工作原理和大。 FooContainer有一個自己的孩子(不是通過this.props.children),它想知道什麼:barId(和:bazId),當這些組件通過其各自的路線加載。有沒有辦法從「父」組件中訪問所有當前參數 - 包括子路由的URL參數 - FooContainer

注意:也有可能我這樣做完全錯誤,在這種情況下,我會喜歡一些更好的方法來實現我的目標。

回答

0

一個答案,對於那些關注:

原來我在孩子尋找this.props.paramsMain不是由任何Route管理,並正因爲如此,我需要明確通支柱params支柱(或其中重要的參數)從Main降至支柱。

Main的實現,作爲一個粗略的草圖JSX,看起來像:

<div> 
    <SomeOtherComponentThatWantsParams /> 
    {this.props.children} 
</div>