
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,我们会在看到邮件的第一时间内为您处理!

Github中国全球用户排名查询、仓库Star排名
免费开源的Vue3桌面端UI组件库Layui Vue中文文档官网介绍
介绍一款快速、简洁且高效的评论系统——Valine
Discord官网:包括Windows/macOS/Linux/Android和iOS下载地址
一款全新的多智能体(Mutli-Agent)框架——AgentScope(魔搭社区)