Not a key value pair?

Continuing the discussion from freeCodeCamp Challenge Guide: Never Mutate State:

why is the “todo” inside the return object not in a key value pair?

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

and also, why doesn’t return state.slice().push(action.todo) work here ?

const immutableReducer = (state = todos, action) => {
  switch (action.type) {
    case ADD_TO_DO:
      // don't mutate state here or the tests will fail

      return state.concat(action.todo);
    // or return [...state, action.todo]

    default:
      return state;
  }
};

That’s just a JS shortcut introduced with ES6. Instead of writing:

return {
    type: ADD_TO_DO,
    todo: todo
  };

You can just use todo by itself, keeps you from having to type it twice (because it’s so much work :slight_smile: ).

1 Like

aaaah, i see. thank you for answering =)

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