Redux: Never Mutate State exercise


I found the mistake! The mistake was this :

const addToDo = (todo) => {
return {
type: ADD_TO_DO,
todo: todo //this was ‘Learn React’ before
}
}

The exercise asked this :

There is a store and reducer in the code editor for managing to-do items. Finish writing the ADD_TO_DO case in the reducer to append a new to-do to the state. There are a few ways to accomplish this with standard JavaScript or ES6. See if you can find a way to return a new array with the item from action.todo appended to the end.

I think I did that but apparently I didn’t, so what I did wrong here?

//mycode here
let newTodos = state.slice();
return newTodos = […state, action.todo];
break;

Code from the exercise :

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:
  //mycode here
    let newTodos = state.slice();
    return newTodos = [...state, action.todo];
    break;

      // don't mutate state here or the tests will fail
    default:
      return state;
  }
};

// an example todo argument would be 'Learn React',
const addToDo = (todo) => {
  return {
    type: ADD_TO_DO,
    todo: 'Learn React'
  }
}

const store = Redux.createStore(immutableReducer);

3 Likes