0 added
0 removed
Original
2026-01-01
Modified
2026-02-21
1
<p><strong>Middlewares</strong>are a much more advanced technique than everything we already know. In this lesson, we'll focus on using them rather than writing them. They will help us to connect various libraries from the beginning of using them in React.</p>
1
<p><strong>Middlewares</strong>are a much more advanced technique than everything we already know. In this lesson, we'll focus on using them rather than writing them. They will help us to connect various libraries from the beginning of using them in React.</p>
2
<p>Middleware is a function that we call sequentially when updating data in the store.</p>
2
<p>Middleware is a function that we call sequentially when updating data in the store.</p>
3
<p>The general principle is like so:</p>
3
<p>The general principle is like so:</p>
4
<ol><li>Middleware is initially built into the repository when we create it</li>
4
<ol><li>Middleware is initially built into the repository when we create it</li>
5
<li>Then, during dispatching, the data passes through the middleware, and only then does it go into the reducer</li>
5
<li>Then, during dispatching, the data passes through the middleware, and only then does it go into the reducer</li>
6
</ol><p>This organization allows programmers to extend libraries with new functionality without rewriting the Redux source code for a specific task.</p>
6
</ol><p>This organization allows programmers to extend libraries with new functionality without rewriting the Redux source code for a specific task.</p>
7
<p>We can use middleware in tasks such as:</p>
7
<p>We can use middleware in tasks such as:</p>
8
<ul><li>Logging</li>
8
<ul><li>Logging</li>
9
<li>Error notification</li>
9
<li>Error notification</li>
10
<li>Working with asynchronous APIs</li>
10
<li>Working with asynchronous APIs</li>
11
<li>Routing</li>
11
<li>Routing</li>
12
</ul><p>How middleware works:</p>
12
</ul><p>How middleware works:</p>
13
<p>Here you can see the<em>thunk</em>middleware. Before passing it to the createStore function, we should apply the other one - applyMiddleware. Note that we're passing the middleware as the second parameter, even though we used initState in the previous lesson.</p>
13
<p>Here you can see the<em>thunk</em>middleware. Before passing it to the createStore function, we should apply the other one - applyMiddleware. Note that we're passing the middleware as the second parameter, even though we used initState in the previous lesson.</p>
14
<p>It happens because the createStore function checks the second parameter's type and understands what's in front of it depending on it.</p>
14
<p>It happens because the createStore function checks the second parameter's type and understands what's in front of it depending on it.</p>
15
<p>If there is more than one piece of middleware, you'll have to use another function:</p>
15
<p>If there is more than one piece of middleware, you'll have to use another function:</p>
16
<p>In this case, we pass the result of the compose function to the store. The compose() function takes middleware as input.</p>
16
<p>In this case, we pass the result of the compose function to the store. The compose() function takes middleware as input.</p>
17
<p>And now we've arrived at the main point. There is a browser extension, Redux DevTools, written for Redux. Install it in your browser.</p>
17
<p>And now we've arrived at the main point. There is a browser extension, Redux DevTools, written for Redux. Install it in your browser.</p>
18
<p>Below is the code for connecting this extension to the repository:</p>
18
<p>Below is the code for connecting this extension to the repository:</p>
19
<p>Note that when we work with a function extension, we don't need applyMiddleware.</p>
19
<p>Note that when we work with a function extension, we don't need applyMiddleware.</p>
20
<p>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. It will be your debugging assistant throughout the course.</p>
20
<p>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. It will be your debugging assistant throughout the course.</p>