// 父組件 class Parent extends Component { handleChild = ()=> { this.$Child.childConsole(); // this上有了子組件方法 } render() { return ( <div> <Child onRef={(ref)=> {this.$Child=ref}} /> <button onClick={this.handleChild}>調用子組件方法</button> </div> ) } } // 子組件 class Child extends Component { componentDidMount() { this.props.onRef(this); // 調用父組件傳入的函數,把自身賦給父組件 } // 定義一個子組件方法 childConsole = ()=> { console.log('我是子組件方法') } render() { return (
<div>
</div>
) }