2016-07-26 90 views
0

如果我有我的路由器以下:React-Router:除了props.children,我可以传递额外的变量给父母吗?

<Route path="/" component={MainLayout}> 
    <Route path="path/one" component={App1} hasTitle={false} /> 
    <Route path="path/two" component={App2} hasTitle={true} /> 
</Route> 

我会在MainLayout得到任何App1App2this.props.children。但是我可以通过在子路线中传递hasTitle=falsehasTitle=true来获得MainLayout中的其他一些定制吗?

+0

传递额外的道具下到孩子,而不是{this.props.children},如果你想从路线的道具,你可以用这个,props.route.hasTitle –

回答

0

使用cloneElementMainLayout

{React.cloneElement(this.props.children, {hasTitle:true})} 
+0

需要指出的如果在'this.props.children'中有多个包含元素,则在克隆之前用'React.Children.forEach'或'React.Children.map'遍历子元素。 – Season

相关问题