咱們閒話很少說,直接上代碼react
// 父組件 import React, {Component} from 'react'; class Parents extends Component { constructor(props) { super(props); this.state = { } } componentDidMount() { } handleCancel = (e) => { console.log('父組件的方法被子組件調用'); } childClick = (e) => { this.child.onShow() } render() { return ( <section> <Child onCancel={this.handleCancel} onRef={(ref)=>{ this.child = ref}}></Child> <div onClick={this.childClick}>調用子組件的函數</div> </section> ); } } export default Parents; // 子組件 import React, {Component} from 'react'; class Child extends Component { constructor(props) { super(props); this.state = { } } componentDidMount() { this.props.onRef(this) } onShow(){ console.log('子組件的方法被父組件調用') } render() { return ( <section> <div onClick={()=>{this.props.handleCancel()}}>子組件用this.props調用父組件的函數</div> </section> ); } } export default Child;