react 父调用子方法(类组件)

316 阅读1分钟

1.父class组件,调用子class组件方法

父组件


this.testRef = React.createRef(); //第一步创建ref

this.testRef.nextFun() //第二步在调用的地方直接使用子组件方法nextFun

<Child
  ref={ node => this.testRef = node }/> // 必要node

子组件

//直接定义方法即可
nextFun = () => {
    console.log(123)
} 

参考:blog.csdn.net/qq_36990322…