Connecting to Redux

Learn how to connect to Redux.

Adding a middleware to the Redux store can be done only during the store creation process. The simplest way to connect middleware to the Redux store is to use the applyMiddleware() store enhancer available as an API from Redux itself.

import { createStore, applyMiddleware } from 'redux';
import reducer from 'reducers/root';
import measureMiddleware from 'middleware/measure';

const store = createStore(reducer, applyMiddleware(measureMiddleware));

The applyMiddleware() function can receive an arbitrary number of middlewares as arguments and create a chain to be connected to the store:

applyMiddleware(middlewareA, middlewareB, middlewareC);

Note: The order of registration is important. The first middleware, in our case middlewareA, will get the action before middlewareB. And if the code there decides to modify or suppress the action, it will never reach either middlewareB or middlewareC.

Get hands-on with 1200+ tech skills courses.