父組件能夠給子組件傳遞一個事件(方法),子組件接收這個事件(方法),調用事件(方法),就會觸發父組件中定義的方法,從而達到改變父組件的statethis
子組件這裏給出關鍵代碼(調用父組件傳遞的方法)code
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> ); } }