Skip to content

Instantly share code, notes, and snippets.

@JackHowa
Last active May 22, 2022 17:49
Show Gist options
  • Save JackHowa/ead36f33879c672d8c5ed937690b654e to your computer and use it in GitHub Desktop.
Save JackHowa/ead36f33879c672d8c5ed937690b654e to your computer and use it in GitHub Desktop.
fcc redux curriculum snippets
this is for the free code camp course on Redux https://learn.freecodecamp.org/front-end-libraries/redux/create-a-redux-store
@JackHowa
Copy link
Author

JackHowa commented Jun 17, 2018

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

  • const newObject = Object.assign({}, obj1, obj2);

This creates newObject as a new object, which contains the properties that currently exist in obj1 and obj2.

  • basically edit the properties mentioned
  • doesn't mutate
const defaultState = {
  user: 'CamperBot',
  status: 'offline',
  friends: '732,982',
  community: 'freeCodeCamp'
};
undefined
console.log(Object.assign({}, defaultState, {status: 'online'}))

{user: "CamperBot", status: "online", friends: "732,982", community: "freeCodeCamp"}
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({}, defaultState, {status: 'online'});
    default:
      return state;
  }
};

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

const store = Redux.createStore(immutableReducer);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment