Blog>
Snippets

Implementing Redux Middlewares in Redux v5.0.0

Illustrate how to apply middlewares like redux-thunk or redux-saga in the new Redux v5.0.0 store configuration.
import { configureStore } from '@reduxjs/toolkit';
import thunk from 'redux-thunk';
import createSagaMiddleware from 'redux-saga';
import rootReducer from './reducers';
import rootSaga from './sagas';

// creating the saga middleware
const sagaMiddleware = createSagaMiddleware();

// configuring the store and adding middlewares
const store = configureStore({
  reducer: rootReducer,
  middleware: (getDefaultMiddleware) => getDefaultMiddleware().concat(thunk, sagaMiddleware),
});

// then run the saga
sagaMiddleware.run(rootSaga);

export default store;
This piece of code imports required modules and creates a Redux store with redux-thunk and redux-saga middleware included. `configureStore` is part of Redux Toolkit which takes care of most setup automatically. `getDefaultMiddleware` includes Redux best practices by default, and `concat` adds the thunk and saga middleware to the existing array of middlewares. The saga middleware then runs using `run()` with the rootSaga, which would control saga side effects.
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Redux Middleware Example</title>
    <script src="path_to_your_bundle.js"></script>
</head>
<body>
    <!-- React Components go here -->
</body>
</html>
This is an HTML boilerplate to hook in a JavaScript bundle that includes Redux with configured middlewares. Generally, React components would mount within the body tag.
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
    'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
    sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

code {
  font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
    monospace;
}
This is a basic CSS snippet for a typical style reset and font definitions that might be included into the project's CSS file to shape how the content is displayed.