React -父組件獲取子組件的值-以及方法


    1. 通過 ref

        <Child  ref="demo" />   給子組件添加ref屬性
      
        在父組件使用 `this.refs.demo.state.xxx` 來獲取子組件state里面的xxx的值
                
               使用 `this.refs.demo.dosomthing()` 來調用子組件的dosomthing()方法
      
        1. 通過 onRef

          <Child onRef={(ref)=>this.child=ref} /> 給子組件添加ref屬性

          在子組件中,
          componentDidMount() {
          this.props.onRef(this);
          }
          在父組件中,
          this.child.state.xxx //獲取
          this.child.dosomthing() // 調用


免責聲明!

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



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