redux-observable-util
Redux Observable Util is a helper library for using Redux and Redux Observable in TypeScript. It contains some helper functions and a simplified Epic setup.
Table of Contents
Peer Dependencies
Module | Version |
---|---|
redux | ^4.0.0 |
redux-observable | ^1.0.0 |
rxjs | ^6.0.0 |
Defining Redux Observable Epics
Redux Observable Util simplifies the setup for defining an epic, cleaning up the code for readability.
Before
somethingEpic = (action$, state$) =>
action$.pipe(
ofType('SOMETHING'),
switchMap(() =>
// Add logic
return { type: 'SUCCESS', something: 'something' };
)
);
After
@Epic('SOMETHING')
somethingEpic(action: AnyAction, state$: any) {
// Add logic
return { type: 'SUCCESS', something: 'something' };
}
Configuring Epics in Store
Configuration of the epics is also simplified compared to the standard setup of Redux Observable. You call the generateEpics instead of combineEpics, and pass the services that contain @Epic decorators.
Before
const epicMiddleware = createEpicMiddleware();
export const store = createStore(rootReducer, applyMiddleware(epicMiddleware));
epicMiddleware.run(combineEpics(Service.epic1, Service.epic2));
After
const epicMiddleware = createEpicMiddleware();
export const store = createStore(rootReducer, applyMiddleware(epicMiddleware));
epicMiddleware.run(generateEpics(Service));