美文网首页
React 父组件调用子组件的方法

React 父组件调用子组件的方法

作者: 40c0490e5268 | 来源:发表于2019-10-24 14:40 被阅读0次

import React, {Component} from 'react';

export default class Parent extends Component {
render() {
return(
<div>
//1
<Child onRef={this.onRef} />
<button onClick={this.click} >click</button>
</div>
)
}
//2
onRef = (ref) => {
this.child = ref
}
//3
click = (e) => {
this.child.myName()
}

}

class Child extends Component {
componentDidMount(){
//4
this.props.onRef(this)
}

myName = () => alert('xiaohesong')

render() {
    return ('woqu')
}

}

相关文章

网友评论

      本文标题:React 父组件调用子组件的方法

      本文链接:https://www.haomeiwen.com/subject/qhzrvctx.html