React: Redux Toolkit
Theory: Extra Reducers
Separating data by slices (really by reducers in Redux) leads to situations when we react to the same action in different parts of the repository. For example, if you delete a post, you must also delete its comments, which are in another slice.
In Redux, we solve it simply by working with the switch — adding a reaction to the desired action by its name. It is no longer possible in the Redux Toolkit because of the solid link between reducers and actions. It is the price we pay for reducing the code.
Redux Toolkit brings us the extraReducers mechanism to react to actions taking place in other slices. It works quite simply. We add the extraReducers property to the slice, through which you can set reactions (reducers) to external actions:
The extra reducers are added as cases to the builder object, modifying it directly. So we don't need to give anything back. Moreover, the builder supports chains, which means we can call addCases one after another builder.addCase().addCase()....
Recommended programs
Completed
0 / 7


