I have the following code in my React component:
class TheComponent extends Component{
constructor(props) {
super(props);
this.state = {
a: '',
b: ''
};
}
output = (data) => {
this.setState({
a: data.a,
b: data.b
});
}
render(){
return(<ChildrenComponent outputValues={this.output.bind(this)}/>);
}
}
When I call the output
method from the same component, it works perfectly well, and the state is changed successfully. But when I call it from my ChildrenComponent
it doesn't work, and the state never changes.
So I've added the following code to the output
function in order to find out what's going on: console.log(this.setState)
. And the result is that the "setState" function is there.
So, if the function setState
is properly bound to output
, why it doesn't work? And why it does works only when output is called from the current component?`