I'm currently writing a small web-app in react and since it's my first time effectively using javascript i'm encountering some weird problems. I have a class A
that should forward some behavior to its child components, but for whatever reason the child components methods can't be found an i receive the is not a function
error.
Here the code:
A.jsx
:
class A extends React.Component{
constructor(props){
super(props);
this.state = {
type:""
};
this.foo = this.foo.bind(this);
this.b = <B onClick={() => this.setState({type: "b"})}/>;
this.c = <C onClick={() => this.setState({type: "c"})}/>;
}
render(){
return (
<SomeExternalStuff
sendRequest={this.foo}
//some props...>
{this.b}
{this.c}
</SomeExternalStuff>
);
}
foo( test ){
console.log(this.deletionButton);
switch(this.state.type){
case "b":
this.b.bar(test);
break;
case "c":
this.c.bar(test);
break;
}
}
}
B.jsx
:
class B extends React.Component{
static TITLE = "<some title>";
render(){
return (
<Button type="submit" variant="danger" onClick={this.props.onClick}>
{B.TITLE}
</Button>
);
}
bar(test){
console.log("I'M IN B");
}
}
C.jsx
is nearly the same as B.jsx
. I get the same error for both components.