[英]Split Transclusion React
我想要做的是让子级组件影响其父级的子级组件。 切实地:我希望内容组件能够将组件注入到预定义的标题和侧边栏元素中。
路由器:
<Router history={browserHistory}>
<Route path="/" component={AppRoot}>
<Route path="/home" component={Home}/>
<Route path="/list" component={List}/>
<Route path="/profile/:uid" component={Profile}/>
</Route>
</Router>
应用根:
class AppRoot extends Component {
constructor() {
super();
this.state = {
header_elements: <div>HELLO???</div>
};
console.log(this);
}
render() {
return (
<div>
<AppBar className="app_bar" title="Soulhart" showMenuIconButton={false}>
<div id="nested_content">
{this.state.header_elements}
</div>
</AppBar>
<div>
{this.props.children}
</div>
</div>
);
}
}
家:
class Home extends Component {
render() {
return (<strong>Home</strong>);
}
}
我的导航和标题是在AppRoot中定义的,但是我不确定如何通过Home设置AppRoot.header_elements值。 有没有更简单的方法可以做到这一点,或者我想要的是不可能的吗?
执行此操作的方法是在子组件上具有func类型的属性-
Home.propTypes = {
updateRoot : React.PropTypes.func
};
然后从家里打电话给他。 当然,您需要将其连接到路由器中
<Route path="home" component={() => <Home updateRoot={this.handleUpdateRoot}/>}/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.