Guest User

Untitled

a guest
Jul 18th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. /* It's easy to avoid mutating arrays using spread operators (const newArr = [...oldArray])
  2. this is how to make sure you only overwrite new values to an object */
  3.  
  4. const defaultState = {
  5. user: 'CamperBot',
  6. status: 'offline',
  7. friends: '732,982',
  8. community: 'freeCodeCamp'
  9. };
  10.  
  11. const immutableReducer = (state = defaultState, action) => {
  12. switch(action.type) {
  13. case 'ONLINE':
  14. // don't mutate state here or the tests will fail
  15. const updatedObj = Object.assign({}, state, {status: 'online'}); //creates new object, starting blank, the updating and overwriting with following objects. The updated status overrides the one of the current state because it follows it.
  16. return updatedObj;
  17. default:
  18. return state;
  19. }
  20. };
  21.  
  22. const wakeUp = () => {
  23. return {
  24. type: 'ONLINE'
  25. }
  26. };
  27.  
  28. const store = Redux.createStore(immutableReducer);
Add Comment
Please, Sign In to add comment