Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const {createStore, applyMiddleware, combineReducers} = require('redux');
- const createSagaMiddleware = require('redux-saga');
- const {put, takeLatest} = require('redux-saga/effects');
- const actions = {
- SUM_UP_TO_TEN: 'SUM_UP_TO_TEN',
- INCREMENT: 'INCREMENT',
- DECREMENT: 'DECREMENT'
- };
- function* somaAteDez(action) {
- while (action.payload < 10) {
- yield put({type: actions.INCREMENT});
- action.payload++;
- }
- }
- function* mySaga() {
- yield takeLatest(actions.SUM_UP_TO_TEN, somaAteDez);
- }
- function reducer1(state = 0, action) {
- if (action.type === actions.INCREMENT)
- return state + 1;
- if (action.type === actions.DECREMENT)
- return state - 1;
- return state;
- }
- function reducer2(state = 0, action) {
- if (action.type === actions.INCREMENT)
- return state + 1;
- if (action.type === actions.DECREMENT)
- return state - 1;
- return state;
- }
- const sagaMiddleware = createSagaMiddleware.default();
- const reducer = combineReducers({reducer1, reducer2});
- let store = createStore(reducer, applyMiddleware(
- sagaMiddleware
- ));
- sagaMiddleware.run(mySaga);
- store.subscribe(() => console.log(store.getState()));
- store.dispatch({type: actions.DECREMENT});
- store.dispatch({type: actions.INCREMENT});
- store.dispatch({type: actions.DECREMENT});
- store.dispatch({type: actions.SUM_UP_TO_TEN, payload: store.getState().reducer1});
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement