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