Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. const {createStore, applyMiddleware, combineReducers} = require('redux');
  2. const createSagaMiddleware = require('redux-saga');
  3. const {put, takeLatest} = require('redux-saga/effects');
  4.  
  5. const actions = {
  6. SUM_UP_TO_TEN: 'SUM_UP_TO_TEN',
  7. INCREMENT: 'INCREMENT',
  8. DECREMENT: 'DECREMENT'
  9. };
  10.  
  11. function* somaAteDez(action) {
  12. while (action.payload < 10) {
  13. yield put({type: actions.INCREMENT});
  14. action.payload++;
  15. }
  16. }
  17.  
  18. function* mySaga() {
  19. yield takeLatest(actions.SUM_UP_TO_TEN, somaAteDez);
  20. }
  21.  
  22.  
  23. function reducer1(state = 0, action) {
  24. if (action.type === actions.INCREMENT)
  25. return state + 1;
  26. if (action.type === actions.DECREMENT)
  27. return state - 1;
  28. return state;
  29. }
  30.  
  31. function reducer2(state = 0, action) {
  32. if (action.type === actions.INCREMENT)
  33. return state + 1;
  34. if (action.type === actions.DECREMENT)
  35. return state - 1;
  36. return state;
  37. }
  38.  
  39. const sagaMiddleware = createSagaMiddleware.default();
  40. const reducer = combineReducers({reducer1, reducer2});
  41.  
  42. let store = createStore(reducer, applyMiddleware(
  43. sagaMiddleware
  44. ));
  45.  
  46. sagaMiddleware.run(mySaga);
  47.  
  48. store.subscribe(() => console.log(store.getState()));
  49.  
  50. store.dispatch({type: actions.DECREMENT});
  51.  
  52. store.dispatch({type: actions.INCREMENT});
  53.  
  54. store.dispatch({type: actions.DECREMENT});
  55.  
  56. store.dispatch({type: actions.SUM_UP_TO_TEN, payload: store.getState().reducer1});
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement