【发布时间】:2016-02-07 12:09:26
【问题描述】:
我有一个主要组件App,根据路线包含一些孩子(我使用react-router)等:
class App extends Component {
otherClick = () => { /* run every children's `handleButton2` function */ }
<div className="App">
<Button handleMenuClick={this.toggleSideBar}>Button 1</Button>
<Button handleOtherClick={this.otherClick}>Button 2</Button>
<SideBar ref="sideBar" title="Toto"/>
{this.props.children}
</div>
}
因此,根据路由,App 将包含一些其他容器,例如:
class ContainerABC extends React.Component {
constructor(props) {
super(props);
}
handleButton2 = () => {
let sc = this.refs.subCont;
sc.setState({visible : !sc.visible});
// Change the color of Button 2 ???
};
render() {
return (
<div>
<SubContainer ref="subCont"/>
</div>
);
}
};
Button 2 的作用取决于当前的 Container。在上面的例子中,当我有一个ContainerABC 作为孩子时,我希望Button 2 切换SubContainer 的ContainerABC。
如何告诉 Button 2 根据组件的子级执行适当的操作?
和/或当Button 2 触发SubCont 上的操作时,如何从SubCont 修改Button 2(或任何触发器)?
也许使用 Redux ?我看不出它有什么帮助
【问题讨论】:
标签: reactjs react-router redux