Universal middleware for adding a layer to execute side effects to Redux.
- Do not pollute actions.
- Hook action and execute callbacks.
- The callbacks can be implemented with
Promise
orAsync/Await
, and are not forced to implement withGenerator
. - In the callbacks, other actions can be dispatched.
- Universal.
yarn add redux-spork
import { fork } from "redux-spork";
const sporks = [
fork("FETCH", async ({ dispatch }) => {
const data = await fetch("./data.json");
dispatch({ type: "DATA", payload: data });
}),
];
import React from "react";
import { render } from "react-dom";
import { Provider } from "react-redux";
import { createStore, applyMiddleware } from "redux";
import { createMiddleware } from "redux-spork";
const middleware = createMiddleware();
middleware.run(sporks);
const store = createStore(reducer, applyMiddleware(middleware));
render(
<Provider store={store}>
<App />
</Provider>,
);