父组件能够给子组件传递一个事件(办法),子组件接管这个事件(办法),调用事件(办法),就会触发父组件中定义的办法,从而达到扭转父组件的state
子组件这里给出要害代码(调用父组件传递的办法)
export default class Loose extends React.Component { constructor(props) { super(props); }; cancel = () => { {/* 这里给子组件 setPare */} this.props.setPare(); }; render() { return ( <button onClick={() => this.cancel()}>勾销</button> ); }}
父组件给出要害代码(给子组件传递办法)
export default class SaleReleaseSeach extends React.Component { constructor(props) { super(props); } aaaa = () => { console.log("子组件在触发调用我"); }; render() { return ( {/* 右拉入 */} <div> <MyRight {...this.props} {/* 这里给子组件提供办法setPare */} setPare={this.aaaa} ></MyRight> </div> ); }}