I got a problem to call a parents method over two layers. It either does nothing or says, that it is not a function. So here is my Child-Component:
class Child extends React.Component {
render() { //return some awesome HTML, that at some point calls doStuff }
doStuff() {
//do some stuff
this.props.parentState.notifyParent();
}
}
My parent component now got some conditional stuff going on, so it looks like this:
class Parent extends React.Component {
constructor(props : any) {
super(props);
this.state = {
//setSomeProps
updateParent: this.updateParent
};
}
determineUsedComponent(props: any) {
//some conditions
return <Child parentState={props}/>
}
updateParent() {
console.log("Update parent");
}
render() {
return (<this.determineUsedComponent props={this.state}/>)
}
}
I also tried giving the function as single object to the child. Do stuff looks the following then:
doStuff() {
props.notifyParent()
}
while the Parent component then has the determineUsedComponent method with following signature:
determineUsedComponent(props: Any, updateParent: Function) {}
and it gets called:
<this.determineUsedComponent props={this.state} updateParent={this.updateParent}/>
If I then try inside the determineUsedComponent method to insert it like an attribute:
return <Child parentUpdate={this.parentUpdate}/>
It again throws the exception "this.props.notifyParent() not a function". Instead if I write it like this:
return <Child parentUpdate={()=>this.parentUpdate}/>
There is no error, instead just nothing happens. (Also no log.) I am very clueless and now tried every possible option of how to write the function and give it to child, but some gave errors (above two) and most didn´t do anything at all. Any ideas? Maybe good to mention: Child component is written in pure JS, while Parent is written in Typescript.