So I am following some React tutorials and the lecturer is trying to explain forward API's and ref API's.
So we have a children which looks like this
import React, { Component } from 'react';
class Cppersons extends Component {
myFocus () {
this.lastref.current.focus()
}
render() {
//something
return (
<div> Something </div>
)
}
}
export default Cppersons;
Note: this in above code
myFocus () {
this.lastref.current.focus()
}
In the above code, we created this.lastref first on the parent component hence I think it is coming from there (passing a ref from parent to child) Here is parent component
import React, { Component } from 'react';
import Person from './persons/person-s';
class Cpersons extends Component {
this.lastref = React.createRef()
componentDidMount() {
this.lastref.current.myFocus()
}
render (
return {
<Person
key={el.id}
click={this.props.cpdelete.bind(index)}
ref={this.lastref}
name={el.name}
age={el.age}
changed={(event) => this.props.cpchanged(event, el.id)} />
});
}
}
export default Cpersons
So my question is that how can we use myFocus in the parent when the method is defined in the children? plus how does myFocus work here?