
react如何实现父组件调用子组件函数,下面web建站小编给大家详细介绍一下实现代码!
实现代码如下:
class father extends Component {
constructer(props) {
super(props);
this.state={
a: '1',
b: '2',
}
this.myRef
this.test = this.test.bind(this)
}
hange() {
const { a,b } = this.state
console.log(this.myRef.test(a,b)) // 直接调用实例化后的Children组件对象里函数
}
render() {
<Children wrappedComponentRef={(inst) => { this.myRef = inst } } ref={(inst) => { this.myRef = inst } } />
<button onClick={this.test}>点击</button>
}
}
ps:wrappedComponentRef是react-router v4中用来解决高阶组件无法正确获取到ref。
标签: react, react-router, 子组件, 父组件
上面是“react如何实现父组件调用子组件函数”的全面内容,想了解更多关于 reactjs 内容,请继续关注web建站教程。
当前网址:https://m.ipkd.cn/webs_2755.html
声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!

dede织梦后台文章增加HTML自定义字段字符被过滤问
推荐一款WinPE启动盘工具神奇——WePETools(微PE工具箱)
数字仓鼠PanSou:一个高性能的网盘资源搜索API服务