Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

213
Visualizações
How to immutable update an object given that we have to pass the same object as props?

I have an object amounts that gets updated on button clicks. Ans i pass that object as a prop to another component. What i am doing right now is updating object in mutable way on button click event.

onClick = e => {
  amounts.map(
      amount => (amount.tax = taxes ? 500 : 0)
  );
}

<Display amounts={amounts} />

How can i update amounts in an immutable way?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

As mentioned in the comments, there are a few things going on:

  1. You are not updating the amounts Array reference, so React will not re-render based on this mutation.
  2. You are using Array#map to update a single property. This will update the Object reference in the amounts collection.
  3. There is no setAmounts or anything similar in order to update the value of the amount property in a parent component.

Assuming you are using useState in the <Display />s parent component, you will have to pass the setAmounts function to the <Display /> component using props.

<Display amounts={amounts} setAmounts={setAmounts} />
onClick = e => {
  setAmounts(
    amounts.map(
      amount => ({ ...amount, tax: taxes ? 500 : 0 })
    );
  );
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda