I am trying to access a child method from the parent component. This is how the child code looks like
class child extends Component {
someMethod(v) {
// do something
}
render(){
return(
...
)
}
}
And this is the parent code
class Parent extends Component {
hoverOn(){
this._Child.someMethod(10); // _Child is undefined here
}
render(){
return(
<div onMouseOver={this.hoverOn.bind(this)}>
<OtherChild ....>
<Child ref={(Child) => { this._Child = Child; }} />
</div>
)
}
}
On hoverOn() I get an error telling me that _Child is undefined. How can I call someMethod() from the Parent Class?
childwhereas it should beChild, other thing is that since you have onMouseOver event which may get triggered before the component is mounted and hence you can get an error so add a conditional check likethis._Child && this._Child.someMethod(10);