Register to get access to free programming courses with interactive exercises

Middlewares JS: Redux (React)

Middleware are a much more advanced technique than what we've looked at previously. In this lesson, we'll focus more on using them, rather than writing them. They'll help us to connect various libraries, and from the very beginning of using them in React.

Middleware are functions that are called sequentially when updating data in the store.

The general principle is like so:

  1. Middleware is initially built into the repository when it's created
  2. Then, during dispatching, the data passes through the middleware and only then does it go into the reducer

Midwives working in Redux

This organization allows programmers to extend libraries with new functionality without rewriting the Redux source code for a specific task.

Middleware is used in tasks such as:

  • Logging
  • Error notification
  • Working with asynchronous APIs
  • Routing

How middleware works:

import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';

const store = createStore(
  /* preloadedState, */

Above you can see the thunk middleware. Before passing it to the createStore function, we need to apply the applyMiddleware function to it. Note that we're passing the middleware as the second parameter, even though in the previous lesson the second parameter was initState. This happens because the createStore function checks the type of the second parameter, and understands what's in front of it depending on the type.

If there is more than one piece of middleware, you'll have to use another function:

import { createStore, applyMiddleware, compose } from 'redux';
import thunk from 'redux-thunk';
import logger from 'redux-logger';

const store = createStore(
  /* preloadedState, */

In this case, the result of the compose function is passed to the store. The compose() function takes middleware as input.

And now we've arrived at the main point. There's a special browser extension, Redux DevTools, written especially for Redux. Install it in your browser.

Extension for Redux

Below is the code for connecting this extension to the repository:

const reduxDevtools = window.__REDUX_DEVTOOLS_EXTENSION__;
const store = createStore(
   /* preloadedState, */
    reduxDevtools && reduxDevtools(),

Note that when we're working with a function extension, we don't need applyMiddleware.

In future lessons, you won't have to connect the extension yourself. We've already done it for you. All you need to do is install it and remember to use it. This will be your main debugging assistant throughout the course.

Recommended materials

  1. ReduxDevTools documentation

Hexlet Experts

Are there any more questions? Ask them in the Discussion section.

The Hexlet support team or other students will answer you.

About Hexlet learning process

For full access to the course you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
hours of theory

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

<span class="translation_missing" title="translation missing:">Bookmate</span>
<span class="translation_missing" title="translation missing:">Healthsamurai</span>
<span class="translation_missing" title="translation missing:">Dualboot</span>
<span class="translation_missing" title="translation missing:">Abbyy</span>
Suggested learning programs

From a novice to a developer. Get a job or your money back!

Frontend Developer icon
Development of front-end components for web applications
start anytime 10 months

Use Hexlet to the fullest extent!

  • Ask questions about the lesson
  • Test your knowledge in quizzes
  • Practice in your browser
  • Track your progress

Sign up or sign in

By sending this form, you agree to our Personal Policy and Service Conditions
Toto Image

Ask questions if you want to discuss a theory or an exercise. Hexlet Support Team and experienced community members can help find answers and solve a problem.