React: Redux Toolkit
Theory: Entity Adapter
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:
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 Entity Adapter mechanism. It provides a set of ready-made reducers and selectors for basic entity operations. First, an example:
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:
Example of its use in the application:
In addition to the selectAll(state) we get:
-
selectIds(state)– returnsids -
selectEntities(state)– returnsentities -
selectTotal(state)– returns the total amount -
selectById(state, id)– returns a specific entity orundefinedif it found nothing
Recommended programs
Completed
0 / 7


