父組件調用子組件的方法

一、通常狀況下父組件調用子組件的方法能夠經過 ref 進行調用 redux

......

// 能夠使用  this.Son.子組件的方法 來進行調用

render() {
    return (
        <div>
            <Son ref ={ (element) => {this.Son = element} }>
        </div>    
    )    
    
......

二、當子組件經過 redux 的 connect 高階組件包裹時,不能經過以上方式直接讀取到子組件,而讀到的是包裹後的 Connect 組件app

  解決辦法以下:this

  1)、首先在子組件在使用 connect 時須要填寫第四個參數spa

......

export default connect(mapStateToPorops,mapDispatchToProps,null,{withRef: true})(Son)

......

  2)、在父組件中調用時須要注意,調用 getWrappedInstance 方法後才能得到真正的 Son 組件code

......

// 能夠使用  this.Son.子組件的方法 來進行調用

render() {
    return (
        <div>
            <Son ref ={ (element) => {this.Son = element.getWrappedInstance()} }>
        </div>    
    )    
    
......
相關文章
相關標籤/搜索