Child.js:
export default class Child extends Component {
testFunc = () => {
console.log("test")
}
componentDidMount() {
this.props.onRef(this)
}
Parent.js:
export default class Parent extends Component {
render() {
return (
<>
<Child onRef = {ref => (this.child=ref)}> </Child>
<Button onPress={this.child.testFunc()}></Button>
</>
)
}
}
I was wondering how do I call a function that is in a child component from parent component in React Native? I tried the code above and I get the error "TypeError undefined is not an object (evaluating '_this.child.testFunc').
I get the same error when I tried the suggested solution here: Call child function from parent component in React Native.
Could someone help me please?