React子组件修改父组件的state

父组件能够给子组件传递一个事件(方法),子组件接收这个事件(方法),调用事件(方法),就会触发父组件中定义的方法,从而达到改变父组件的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>
    );
  }
}
相关文章
相关标签/搜索