I am trying to add each value entered in separate input element which is added on clicking "add" button on GUI, into an array.
There is no initial array, so please do not suggest putting index from any mapping of initial array.
Multiple input elements are added when user clicks on add button on GUI, the method subDivs() below explains that.
So I am passing counter value as an index in onChange handler, but this code does not work. Really looking for some productive inputs, maybe I am missing some essential part.
handleChange(event, index) {
event.preventDefault();
let { nameArray } = this.state;
nameArray[index] = event.target.value;
this.setState({nameArray}, () => console.log("Array ", nameArray))
}
addRow() {
this.setState({ count: this.state.count + 1 })
};
subDivs() {
let count = this.state.count, uiItems = [];
var { names } = this.state;
let options2 = names.map(name2 => {
return { value: name2.name, label: name2.name };
})
while (count--)
uiItems.push(
<div className="newName" key={this.nextUniqueId()}>
<div className="input-group mb-3" >
<input id={count+1} type="text" className="form-control" placeholder="Enter Name"
onChange={(e) => this.handleChange(e, count+1)}
/>
</div>
</div>
)
return uiItems;
}