Redux doesn’t handle async work too well. Thunk is the go-to solution, but it’s not always great for testing.
Here is how to do a basic async data fetch with redux observable;
export const exampleEpic = (action$, state$, { later }) =>
action$.pipe(
ofType('projects/updateTitle'),
debounceTime(1000),
switchMap(({ payload }) =>
from(later(2000, payload)).pipe(
map(res => ({
type: 'projects/fetchFulfilled',
payload: res,
}))
)
)
);
Thunks are called epics in redux observable. All epics take three parameters (action$, state$, { dependancies })
. The last two are optional.
The action$
parameter is a stream of all redux actions emitted over time. state$
is the state of your redux store. dependencies
can contain any side effects that you want to use in your epic. Passing in side effects as dependencies is super handy...