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