Never Mutate State using Object.assign() and can't figure out what I did wrong

Never Mutate State using Object.assign() and can't figure out what I did wrong
0

#1

Tell us what’s happening:
I’m trying to use Object.assign() to finish this problem, because in theory what I’m thinking is that I need to return a new state with the old todos and the new todo tacked on the end. I should then be able to assign a new empty object, spread the existing todos and tack on a new todo action object? It doesn’t really make 100% sense in my head since the existing state is an array not an object, and the action returns objects not arrays but I’m having trouble thinking of how I should be solving it.

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 Object.assign(
        {},
        ...state,
        {
          todo: 'Go places'
        }
      )
      return
    default:
      return state;
  }
};

// an example todo argument would be 'Learn React',
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_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/front-end-libraries/redux/never-mutate-state


#2

You have the right idea, but the wrong data structure. Think about how you could create a new array with the new todo added to the end.


#3

Turns out I was way overthinking it. Thanks for the push in the right direction!