Answer the question
In order to leave comments, you need to log in
How to correctly hang onChange on input in React.js?
Hello.
There are two buttons. Depending on which button I clicked, text fields with data are rendered.
var Data = [1, 2, 3];
var Test = ['Vasya', 'Kate', 'TEst'];
var Main = React.createClass({
getInitialState: function () {
return {
inputs: null
}
},
_getData: function () {
this.setState({
inputs: Data
});
},
_getTest: function () {
this.setState({
inputs: Test
});
},
createBtnData: function () {
return <button onClick={this._getData}>Get Data</button>;
},
createBtnTest: function () {
return <button onClick={this._getTest}>Get Test</button>;
},
_changeValue: function (e) {
var val = e.target.value;
this.setState({
inputs: val
});
},
createInput: function () {
if (this.state.inputs) {
return this.state.inputs.map(function (item, i) {
return <input key={i} value={item} onChange={this._changeValue} />
}.bind(this));
}
},
render: function() {
return <div>
{this.createBtnData()}
{this.createBtnTest()}
{this.createInput()}
</div>;
}
});
TypeError: this.state.inputs.map is not a function
Answer the question
In order to leave comments, you need to log in
For example, this is jsfiddle.net/VovanR/0btxjm5c We
replace the value of the element by index and update the state.
Perhaps there is another way.
You have an error in your code. Initially, the inputs state variable is an array. But in the onChange event handler, you are assigning a string to inputs. The string does not have a map method which is used in createInput
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question