import React, {Component} from 'react';
export default class Parent extends Component {
onRef = ref => {
this.childRef = ref
}
click = () => {
this.childRef.sayHello()
}
render() {
return (
<div>
<Child onRef={this.onRef} />
<button onClick={this.click}>click</button>
</div>
)
}
}
class Child extends Component {
componentDidMount(){
this.props.onRef(this)
}
sayHello = () => alert('Hello')
render() {
return 'Hello'
}
}
React父组件调用子组件方法
最新推荐文章于 2024-08-27 16:39:52 发布