您需要从父级回传到子回调函数,然后在子级中调用它.
例如:
class Parent extends React.Component { constructor(props){ super(props); this.state = { show: false }; } updateState = () => { this.setState({ show: !this.state.show }); } render() { return (); } } class Child extends React.Component { handleClick = () => { this.props.updateState(); } render() { return ( ); } }
要从子级调用父级方法,您可以像这样传递引用。
家长班
(this.parentReference = ref)} parentReference = {this.parentMethod.bind(this)} /> parentMethod(data) { }
儿童班
let data = { id: 'xyz', name: 'zzz', };
//这将调用parent的方法
this.props.parentReference(data);