-1
I want to update only some attributes of an object, these attributes are varied, one time I want to update some, another time. Follow the example:
const [myObject, setMyObject] = useState({
apartments: 0,
hoods: 0,
elevators: 0,
heating: 0,
})
So suppose I have the following other state:
let novaAtualizacao = [{ elevators: 5, heating: 1}]
That’s what I want myObject
update only what is on novaAtualizacao
. I tried so:
setMyObject(prevState => {
return { ...prevState, novaAtualizacao }
});
But it didn’t happen. Someone can help me?
novaAtualizacao
is an array, so Voce should select the first element and then perform the de-structuring. Something like:return { ...prevState, ...novaAtualizacao[0] }
, values will be updated according to the values innovaAtualizacao
. Another thing, if Voce will adjust a new state, usesetMyObject
and notmyObject
.– Cmte Cardeal
I did the de-structuring but is only taking the first element of newupgrade, being that I need all that are inside it
– Rindi