I have two simple reactjs components with a parent-child relationship. The child contains an input with an onChange event and a callback to the parent to setState. When I run it, setState appears to work in the parent component, but the props passed into the child component don't appear to be updating. Any ideas? Here's a code snippet:
var FooInput = React.createClass({
handleInputChange: function(e) {
this.props.onInputChange(e.target.name, e.target.value);
},
render: function() {
return (
<div>
<input name="foo" value={this.props.foo} onChange={this.handleInputChange} />
</div>
)
}
})
var FooApp = React.createClass({
getInitialState: function() {
return {
foo: 'bar',
};
},
handleInputChange: function(key, value) {
this.setState({key: value});
},
render: function() {
return (
<div>
<FooInput
foo={this.state.foo}
onInputChange={this.handleInputChange} />
</div>
);
}
});
In the example above, the value of input in the FooInput
child component stays as "bar" despite the fact that handleInputChange
is triggered in the FooApp
parent component and foo is passed into the child component via this.props
. Why doesn't the input value change to reflect the new state?