For child-parent communication you should pass a function setting the state from parent to child, like this
(对于儿童与父母之间的通信,您应该传递一个将状态从父母更改为孩子的函数,如下所示)
class Parent extends React.Component { constructor(props) { super(props) this.handler = this.handler.bind(this) } handler() { this.setState({ someVar: 'some value' }) } render() { return <Child handler = {this.handler} /> } } class Child extends React.Component { render() { return <Button onClick = {this.props.handler}/ > } }
This way the child can update the parent's state with the call of a function passed with props.
(这样,孩子可以通过调用通过props传递的函数来更新父母的状态。)
But you will have to rethink your components' structure, because as I understand components 5 and 3 are not related.
(但是您将不得不重新考虑组件的结构,因为据我了解,组件5和3并不相关。)
One possible solution is to wrap them in a higher level component which will contain the state of both component 1 and 3. This component will set the lower level state through props.
(一种可能的解决方案是将它们包装在更高级别的组件中,该组件将同时包含组件1和3的状态。此组件将通过prop设置较低级别的状态。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…