Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

196
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!