16.07 Handling Asynchronous Code
Installing redux-thunk
- npm
- Yarn
npm install --save redux-thunk
yarn add redux-thunk
index.js
index.js
import React from "react";
import ReactDOM from "react-dom";
import { Provider } from "react-redux";
import { applyMiddleware, combineReducers, compose, createStore } from "redux";
import thunk from "redux-thunk";
import App from "./App";
import "./index.css";
import registerServiceWorker from "./registerServiceWorker";
import counterReducer from "./store/reducers/counter";
import resultReducer from "./store/reducers/result";
const rootReducer = combineReducers({
ctr: counterReducer,
res: resultReducer,
});
const logger = (store) => {
return (next) => {
return (action) => {
console.log("[Middleware] Dispatching", action);
const result = next(action);
console.log("[Middleware] next state", store.getState());
return result;
};
};
};
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const store = createStore(
rootReducer,
composeEnhancers(applyMiddleware(logger, thunk))
);
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById("root")
);
registerServiceWorker();
actions.js
actions.js
export const INCREMENT = "INCREMENT";
export const DECREMENT = "DECREMENT";
export const ADD = "ADD";
export const SUBTRACT = "SUBTRACT";
export const STORE_RESULT = "STORE_RESULT";
export const DELETE_RESULT = "DELETE_RESULT";
export const increment = () => {
return {
type: INCREMENT,
};
};
export const decrement = () => {
return {
type: DECREMENT,
};
};
export const add = (value) => {
return {
type: ADD,
value: value,
};
};
export const subtract = (value) => {
return {
type: SUBTRACT,
value: value,
};
};
export const saveResult = (res) => {
return {
type: STORE_RESULT,
result: res,
};
};
export const storeResult = (res) => {
return (dispatch) => {
setTimeout(() => {
dispatch(saveResult(res));
}, 2000);
};
};
export const deleteResult = (resElId) => {
return {
type: DELETE_RESULT,
resultElId: resElId,
};
};