2024年09月26日 建站教程
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。
本文链接:http://so.lmcjl.com/news/13951/