Redux: Never Mutate State_Requesting Help

Hello. I wondered if I could get some help with my code. Although I’m using the slice method to create a new array and to not mutate the existing array, I’m unable to clear the last test. What might I be doing wrong? Thanks!

https://learn.freecodecamp.org/front-end-libraries/redux/never-mutate-state

Code snippet:

const immutableReducer = (state = todos, action) => {
  switch(action.type) {
    case ADD_TO_DO:
      const newArr = state.slice(0);
      newArr.push(action.todo);
      return newArr;
    default:
      return state;
  }
};

Full code

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:
      const newArr = state.slice(0);
      newArr.push(action.todo);
      return newArr;
    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);

You’ve modified the action creator:

const addToDo = (todo) => {
  return {
    type: ADD_TO_DO,
    **todo: 'Learn React'**
}
}

Leave as it is, it was just an example:

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