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)
  }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM