import React, { MouseEvent } from 'react'; import SButton from '@components/SButton'; import Box2 from './box2'; import './style.less'; interface Props {} interface State { box1Ref: React.RefObject; box2Ref: React.RefObject; sayHello: (param: string) => void; } class TestRef extends React.Component { constructor(props: Props) { super(props); this.state = { box1Ref: React.createRef(), box2Ref: React.createRef(), sayHello: this.sayHello }; this.onDosome = this.onDosome.bind(this); this.onDosome2 = this.onDosome2.bind(this); } render() { const { sayHello } = this.state; return (
box
); } onDosome() { console.log(this.state.box1Ref); } onDosome2() { this.state.box2Ref.current.changeInfo(); } sayHello(param: string) { console.log('子组件调用了父组件的 sayHello: ' + param); } } export default TestRef;