Copy an Object with Object.assign

Tell us what’s happening:
I really don’t get why I keep getting this “read only” data error on status. Isn’t the point of the function to over write the previous object’s status? Please help me out if you can.

Your code so far


const defaultState = {
  user: 'CamperBot',
  status: 'offline',
  friends: '732,982',
  community: 'freeCodeCamp'
};

const immutableReducer = (state = defaultState, action) => {
  switch(action.type) {
    case 'ONLINE':
      // don't mutate state here or the tests will fail'
      return Object.assign(state, {status: action.type});
    default:
      return state;
  }
};

const wakeUp = () => {
  return {
    type: 'ONLINE'
  }
};

const store = Redux.createStore(immutableReducer);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/front-end-libraries/redux/copy-an-object-with-object-assign

2 things

  1. Object.assign merges objects into the target object, and returns the target object. So here you’re merging into state, effectively mutating it. See if you could merge into some other target, and return that.
  2. the instructions require you to set the status to ‘online’, look into what action.type returns.

Thank you so much! I ended up getting it.

1 Like