React中遇到的问题

父=>子组件通讯

父组件:

1
2
3
4
5
6
7
8
9
10
11
//元素:
<child onRef={this.onRef} />
<p onClick={this.click.bind(this)}>父组件的点击事件</p>

//方法:
click = () =>{
this.child.childFunction()
}
onRef = (ref) =>{
this.child=ref
}

子组件:

1
2
3
4
5
//方法:
componentDidMount(){
this.props.onRef(this);
}
childFunction(){ }