'ReactJs how to replace new values in a map
I have the below object. Need to replace values one of the keys ( values)
var model ={
n1 :{ values:{a1, b1},
temp :set
},
n2 :{ values:{true},
temp :set
}
}
I want replace value for key n1 as
n1:{
values:{true, false}, temp :set
}
please suggest the correct way of doing this
Solution 1:[1]
The correct way to do this, please look into this code -
This will change exact the values inside n1. the rest of the data will remain same as current state. Here useState used for seeing change in client layout.
const model ={
n1 :{
values:{a1, b1},
temp :set
},
n2 :{
values:{a1},
temp : set
}
}
const [state, setState] = useState(model);
const handleAction = (newValues) => {
setState({
...state,
n1: {
values: {a1: newValues.a1, b1: newValues.b1}
...state.n1,
}
})
}
If you don't need to visualize it in client layout. Just look into this below code -
let model ={
n1 :{
values:{a1, b1},
temp :set
},
n2 :{
values:{a1},
temp : set
}
}
model = {
...model,
n1: {
values: {a1: false, b1: false}
...model.n1,
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
