React父组件调用子组件中的函数

第一步在父组件中写下面代码

  onRef = (ref) => {
    this.child = ref
  };

第二步将此方法传个子组件

 <SelectModal
          onRef={this.onRef}
         
        />

第三步在子组件中实现以下代码

 componentDidMount() {
    this.props.onRef(this)
  }

第四步在父组件调用子组件某个函数

//restInfo是我的方法 你自己写自己的方法 
this.child.restInfo();

 

猜你喜欢

转载自blog.csdn.net/weixin_39706415/article/details/100519048
今日推荐