ref的作用在於父組件取到子組件的所有state內容。
在父組件中 編寫如下:
1. public child: any; 類中定義child,用於存放子組件的作用域
2. 綁定子組件作用域
public onRef(ref: any) {
this.child = ref
}
3. 子組件中綁定ref <Childpage
onRef={this.onRef} /
>
4. onRef 綁定this
this.onRef = this.onRef.bind(this)
在子組件中 編寫如下:
在constructor 中 onRef 綁定this
this.props.onRef(this)
之后在 父組件中可以隨便調用子組件的state中的值 , 調用方法
this.child.state.......
