-
-
Save vkarpov15/658b619a1a12d8c3bc449eaf7dda3781 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { createEpicMiddleware } = require('redux-observable'); | |
const { filter, map } = require('rxjs/operators'); | |
const redux = require('redux'); | |
// An 'epic' takes a single parameter, `action$`, which is an RxJS observable | |
// that represents the stream of all actions going through Redux | |
const countEpic = action$ => action$.pipe( | |
filter(action => action.type === 'CLICK_INCREMENT'), | |
map(action => { | |
return { type: 'INCREMENT', amount: 1 }; | |
}) | |
); | |
const observableMiddleware = createEpicMiddleware(); | |
const store = redux.createStore(reducer, redux.applyMiddleware(observableMiddleware)); | |
// **Must** add the epic to the observable after calling `applyMiddleware()`. | |
// Otherwise you'll get a warning: "epicMiddleware.run(rootEpic) called before | |
// the middleware has been setup by redux. Provide the epicMiddleware instance | |
// to createStore() first" | |
observableMiddleware.run(countEpic); | |
// Sample Redux reducer | |
function reducer(state = 0, action) { | |
console.log('Action', action); | |
switch (action.type) { | |
case 'INCREMENT': | |
return state + action.amount; | |
default: | |
return state; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment