react 父组件如何调用子组件的方法

父组件

 bindRef (ref) {
    this.child = ref
  }
  clickMethod = () => {
    this.child.childMethod()
  }
  render () {
    return (
      <div className='wrapper'>
        <p onClick={this.clickMethod}>这是一个click事件</p>
        <child onRef={this.bindRef.bind(this)}></child>
      </div>
    )
  }

子组件

 childMethod () {
    console.log('我是子组件的方法')
  }
  componentDidMount () {
    this.props.onRef(this)
  }

 

posted @ 2020-07-15 00:24  panchunxin  阅读(4900)  评论(0)    收藏  举报