Consider this example:
var Field = React.createClass({
render: function () {
// never renders new value...
return (
<div>
<input type="text" defaultValue={this.props.value || ''} />
</div>
);
}
});
var App = React.createClass({
getInitialState: function () {
return {value: 'Hello!'};
},
changeTo: function (str) {
this.setState({value: str});
},
render: function () {
return (
<div>
<Field value={this.state.value} />
<button onClick={this.changeTo.bind(null, 'Whyyyy?')}>Change to "Whyyyy?"</button>
<button onClick={this.changeTo.bind(null, void 0)}>Change to undefined</button>
</div>
);
}
});
React.render(
<App />,
document.getElementById('app')
);
I want to pass value into defaultValue
as prop of dumb input component. However it never re-renders it.
I'm fairly certain this has to do with Controlled vs. Uncontrolled inputs.
If I understand correctly, since your
<input>
is Uncontrolled (doesn't define avalue
attribute), then the value will always resolve to the value that it is initialized with. In this caseHello!
.In order to overcome this issue, you can add a
value
attribute and set it during theonChange
:Here is a plunker showing the change.