1

I have below code

case 'COMPLETE_TODO' :
state.todos[state.todos.findIndex((obj => obj.id == action.value))].status = "COMPLETED"
return {
   ...state,
   todos: state.todos
}

I feel like an array is not taken as a modified array because just a property of one single element has been updated.

Any idea?

Thanks

1
  • 2
    Do not mutate state, make a copy of state and then perform operations on it Commented Sep 27, 2019 at 1:51

1 Answer 1

2

Do not mutate state, make a copy of state and then perform operations on it

case 'COMPLETE_TODO' :   
 return {
    ...state,
    todos: state.todos.map(obj=> ({...obj, status: obj.id == action.value ? "COMPLETED" : obj.status}))
 }

map create a new array, ... spread syntax creates a shallow copy, if you object is deeper then one level, then you should do a deep clone,

For deep cloning you can use

 let deepCopy = JSON.parse(JSON.stringify(state.todos))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.