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()
博客园作者:herry菌朋友,看到这里,关注作者的公众号吧,不漏掉更新哦