I want to update value of one object only but updating value of one Object, Updates the value for all objects.
let default = {
name: \'\',
age: \'\'
}
th
I would use the Array.prototype.map function with combination of the object spread syntax (stage 4):
Note that i changed the name of the default
object to obj
.
default
is a reserved key word in javascript
let obj = {
name: '',
age: ''
}
this.state = {
values: Array(2).fill(obj)
}
updateName(event){
const {id, value} = event.target;
this.setState(prev => {
const {values} = prev;
const nextState = values.map((o,idx) => {
if(idx !== id)
return o; // not our object, return as is
return{
...o,
name: value;
}
});
return{
values: nextState
}
});
}