I has a list of object stored in the state. I want to store input data into the object in a format like this.
{
"parameter" : [{
"id":"parameter1",
"value":"value1"
}, {
"id":"parameter2",
"value":"value2"
}]
}
The thing is the number of input is not constant. If there are 10 inputs, my object length should be 10.
(1) type "value1" in a input id=parameter1
{
"parameter" : [{
"id":"parameter1",
"value":"value1"
}]
}
(2) type "value2" in a input id=parameter2
{
"parameter" : [{
"id":"parameter1",
"value":"value1"
}, {
"id":"parameter2",
"value":"value2"
}]
}
(3) type "value3" in a input id=parameter1
{
"parameter" : [{
"id":"parameter1",
"value":"value3"
}, {
"id":"parameter2",
"value":"value2"
}]
}
I wrote onchange handler function but the length of the object always equal to 1. It changed id and value of my object only.
onChangeHandlerParameter = e => {
this.setState({ parameter:[{"id": e.target.id,"value": e.target.value}]})
}
(1) type "value1" in a input id=parameter1
{
"parameter" : [{
"id":"parameter1",
"value":"value1"
}]
}
(2) type "value2" in a input id=parameter2
{
"parameter" : [{
"id":"parameter2",
"value":"value2"
}]
}
parameter. At the moment you're overwriting the existing array with a new one.