Hello. There are two buttons. Depending on which button I clicked, the text fields with the 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>; } }); The problem is that when I click on one of the fields and try to enter text, the following error is caused:
TypeError: this.state.inputs.map is not a function
I understand that the problem is that the map method is trying to be called again for the data that I enter, and since this is not an array, but a string, this is where the error comes from.
Question: how in this case should I correctly hang the onChange handler on <input> so that the code worked correctly, that is: rendered the necessary data, made changes to them, if necessary rendered others. Thanks for the help.
inputspushing a line, if it is assumed that there will be an array? Yes, weak typing and not such obscenities allows you to create, but there is little good in it. I did n’t study ReactJS , so I could n’t fully realize the code, but still: if you need to put only one value into theinputs, then place it as an array of one element. And if you still want to push everything into the variable, then use only the methods available to all possible objects. - Regent_changeValue. I think this is nonsense. - Dmitriy Simushev