Redux - Never Mutate State

I tried to copy the array by the […arr] method and even creating a new variable like:

let newArr = [...state];
return newArr.concat(action.todo);

However, it still tell me it is wrong. Can someone tell me why please?
Thank you in advance.

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:
      // Don't mutate state here or the tests will fail
      return [...state,  action.todo];
    default: 
      return state;
  }
};

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

const store = Redux.createStore(immutableReducer);

console.log(store.getState());
store.dispatch(addToDo('Practice fCC'));
console.log(store.getState());

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36

Challenge: Redux - Never Mutate State

Link to the challenge:

That store.dispatch you added at the end is interfering with tests.

Yes, good catch by @jenovs.

And with that fix, I can confirm that your alternate way of concatenating the value works, too.

I put the code behind [spoiler][/spoiler] tags since it is basically a working solution.

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