react父组件调用子组件的方法

父组件

import React,{Component} from 'react';
import Child from './Child'
export default class Parent extends Component{
  componentDidMount () {
    this.child.test('父组件参数')
  }
  onRef = (ref)=> {
    this.child=ref//接收子组件this
  }
  render () {
    return (
      
) } };

子组件


import React,{Component} from 'react';
export default class Child extends Component{
  componentDidMount () {
    this.props.onRef(this)//传递子组件this
  }
  test = (val)=> {
    console.log(val)
  }
  render () {
    return (
      
子组件
) } };

你可能感兴趣的:(react,react.js)