0 added
0 removed
Original
2026-01-01
Modified
2026-02-21
1
<p>A big part of any web application comes down to operations on entities, adding, changing, deleting, and reading them. For example, in every Hexlet lesson, you can find a discussion section and ask some questions. This section of the front end handles posts, comments, authors, and likes. Thanks to data normalization, the code for processing these entities looks identical:</p>
1
<p>A big part of any web application comes down to operations on entities, adding, changing, deleting, and reading them. For example, in every Hexlet lesson, you can find a discussion section and ask some questions. This section of the front end handles posts, comments, authors, and likes. Thanks to data normalization, the code for processing these entities looks identical:</p>
2
<p>We will use the same code for all other entities. Is there any way to reuse it? Of course! Redux Toolkit does this using the<strong>Entity Adapter mechanism</strong>. It provides a set of ready-made reducers and selectors for basic entity operations. First, an example:</p>
2
<p>We will use the same code for all other entities. Is there any way to reuse it? Of course! Redux Toolkit does this using the<strong>Entity Adapter mechanism</strong>. It provides a set of ready-made reducers and selectors for basic entity operations. First, an example:</p>
3
<p>Just four lines in the reducers brought us a full implementation of standard operations on the user. But that is not all. Besides ready-made reducers, Entity Adapter gives us a set of ready-made selectors to retrieve data from the store. To do this, they must be generated and exported from the file with the slice:</p>
3
<p>Just four lines in the reducers brought us a full implementation of standard operations on the user. But that is not all. Besides ready-made reducers, Entity Adapter gives us a set of ready-made selectors to retrieve data from the store. To do this, they must be generated and exported from the file with the slice:</p>
4
<p>Example of its use in the application:</p>
4
<p>Example of its use in the application:</p>
5
<p>In addition to the selectAll(state) we get:</p>
5
<p>In addition to the selectAll(state) we get:</p>
6
<ul><li><p>selectIds(state) - returns ids</p>
6
<ul><li><p>selectIds(state) - returns ids</p>
7
</li>
7
</li>
8
<li><p>selectEntities(state) - returns entities</p>
8
<li><p>selectEntities(state) - returns entities</p>
9
</li>
9
</li>
10
<li><p>selectTotal(state) - returns the total amount</p>
10
<li><p>selectTotal(state) - returns the total amount</p>
11
</li>
11
</li>
12
<li><p>selectById(state, id) - returns a specific entity or undefined if it found nothing</p>
12
<li><p>selectById(state, id) - returns a specific entity or undefined if it found nothing</p>
13
</li>
13
</li>
14
</ul>
14
</ul>