Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

how to update specific key of an object in reducer

case COMPLETE_TODO: {
  const index = state.todos.findIndex(todo => todo.id === action.payload); //finding index of the item 
  const newArray = [...state.todos]; //making a new array 
  newArray[index].completed = true//changing value in the new array 
  return {   ...state, //copying the orignal state  
          todos: newArray, //reassingning todos to new array 
         }
}
 
PREVIOUS NEXT
Tagged: #update #specific #key #object #reducer
ADD COMMENT
Topic
Name
8+5 =