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

194
Visualizações
Why this way of non-mutating works in React.js?

I'm trying to understand why this example is a good idea. I'm learning React and I found this function which changes the complete property from todo. My question is why this would work? Because we have the const todo which is the one being changed in reality, not the newTodoList which is the one that we return back. I'm guessing that the respective todo from newTodoList is changed as well somehow but I don't undertand how this works because again, we changed the newly created const todo

function toggleTodo(id){
    const newTodoList = [...todos] 
    const todo = newTodoList.find(todo => todo.id === id)
    todo.complete = !todo.complete
    setTodos(newTodoList)
  }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

According to MDN Web Docs on const, The const declaration creates a read-only reference to a value. It does not mean the value it holds is immutable—just that the variable identifier cannot be reassigned. For instance, in the case where the content is an object, this means the object's contents (e.g., its properties) can be altered.

Since const todo is an object, you can change its properties (i.e. todo.complete)

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