1、一般情況下父組件調用子組件的方法可以通過 ref 進行調用
...... // 可以使用 this.Son.子組件的方法 來進行調用 render() { return ( <div> <Son ref ={ (element) => {this.Son = element} }> </div> ) ......
2、當子組件通過 redux 的 connect 高階組件包裹時,不能通過以上方式直接讀取到子組件,而讀到的是包裹后的 Connect 組件
解決辦法如下:
1)、首先在子組件在使用 connect 時需要填寫第四個參數
...... export default connect(mapStateToPorops,mapDispatchToProps,null,{withRef: true})(Son) ......
2)、在父組件中調用時需要注意,調用 getWrappedInstance 方法后才能獲得真正的 Son 組件
...... // 可以使用 this.Son.子組件的方法 來進行調用 render() { return ( <div> <Son ref ={ (element) => {this.Son = element.getWrappedInstance()} }> </div> ) ......