How to complete "Never Mutate State"

Tell us what’s happening:
What is the todo item?
Your code so far


const ADD_TO_DO = 'ADD_TO_DO';

// A list of strings representing tasks to do:
const todos = [
'Go to the store',
'Clean the house',
'Cook dinner',
'Learn to code',
];

const immutableReducer = (state = todos, action) => {
switch(action.type) {
  case ADD_TO_DO:
  return (
    todos
  );
  break;
    // Don't mutate state here or the tests will fail

  default:
    return state;
}
};

const addToDo = (todo) => {
return {
  type: ADD_TO_DO,
  todo
}
}

const store = Redux.createStore(immutableReducer);

  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36.

Challenge: Never Mutate State

Link to the challenge:

return (todos)

with this you’re returning same state every time addTodo is called without ever adding the new todo meant to be added.

To resolve this, you can use

return [...todos, action.todo]

With this you never modify state directly, instead, you return a new copy of state. (as stated in challenge text)

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.