react-父子组件之间的交互传值

子组件要调用父组件的方法:

父组件传入子组件要调的方法

<Child fun={this.fun.bind(this)}>

子组件可通过props直接调用

this.props.fun(xx)

 

父组件要调用子组件的方法:

父组件中给子组件设置onRef事件,并设置调用名=ref

<Child onRef={ref=>this.child=ref} />

子组件中在componentDidMount中调用this.props.onRef(this)

this.props.onRef(this)

这样父组件就可以调用子组件的任意方法了。

this.child.fun1()

 

posted @ 2020-09-12 13:59  herry菌  阅读(218)  评论(0编辑  收藏  举报