Redux - Use the Spread Operator on Arrays

Tell us what’s happening:
I have the following code and I ‘think’ I am copying the array right way but is not letting me pass the test

Dispatching an action of type ADD_TO_DO on the Redux store should add a todo item and should NOT mutate state.

What am I missing?

Your code so far

const immutableReducer = (state = ['Do not mutate state!'], action) => {
  switch(action.type) {
    case 'ADD_TO_DO':
      // Don't mutate state here or the tests will fail
      let copiedState = [...state];
      return copiedState;
    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 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36

Challenge: Redux - Use the Spread Operator on Arrays

Link to the challenge:

You need to add the new todo item to the end of your copied state array.

do you mean like [...state, 'next item'] ?

sorry I got it now [...state, action.todo]

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