Guest User

Untitled

a guest
Nov 22nd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. import { createStore } from 'redux';
  2.  
  3. // Reducer
  4. const initialState = {
  5. switch: false,
  6. name: ''
  7. }
  8.  
  9. function reducer (state = initialState, action = {}) {
  10. switch (action.type) {
  11. case 'flip':
  12. return {
  13. ...state,
  14. switch: !state.switch,
  15. name: action.payload.name
  16. }
  17. default:
  18. return state
  19. }
  20. }
  21.  
  22. // Store
  23. let store = createStore(reducer);
  24.  
  25. store.getState() // { switch: false }
  26.  
  27. const myaction = {
  28. type: 'flip',
  29. payload: {
  30. name: 'kieran'
  31. }
  32. }
  33.  
  34. // Action creator
  35. function flipSwitch(name) {
  36. return {
  37. type: 'flip',
  38. payload: {
  39. name
  40. }
  41. }
  42. }
  43.  
  44. store.dispatch(flipSwitch('kieran')) // with action creator
  45. store.dispatch(myaction) // Normal action
Add Comment
Please, Sign In to add comment