1

父组件:

import React, { Component } from "react";
import Child from './child'
export default class Parent extends Component {

componentDidMount(){
    //被调用的子组件
    this.child.test('参数')
}

onRef(ref){this.child = ref}

render() {
    return (
      <div>
        <Child onRef={this.onRef} />
      </div>
    );
  }
}

子组件:

import React, { Component } from "react";

export default class Child extends Component {

componentDidMount(){
    this.props.onRef(this)
}
//子组件被调用方法
test(val){
    alert('我是测试方法'+val)
}

render() {
    return (
      <div>
            我是子组件
      </div>
    );
  }
}


别把代码当饭吃
125 声望15 粉丝

笨鸟先飞