import React, { Component, Fragment } from 'react';
class Child extends Component {
componentDidMount() {
this.props.onRef(this);
}
testEvent = () => {
console.log('我被触发了');
}
render() {
return (
'我是子组件'
)
}
}
export default class Parent extends Component {
onRef = (ref) => {
this.child = ref;
}
handleClick = () => {
this.child.testEvent();
}
render() {
return (
<Fragment>
<Child onRef={this.onRef} />
<button onClick={this.handleClick}>click</button>
</Fragment>
)
}
}