Rules for Reducers
There are certain rules while using Redux Reducers which we need to follow to avoid certain bugs or surprises.
- Reducers should use only the
stateandactionarguments to calculate the new state. They shouldn't access any external values; - Reducers must not perform asynchronous logic or side effects such as making API calls or running timers etc.;
- The state should not be directly modified inside the reducer functions, instead, the reducer function should return a duplicate state object with updated values. This rule only applies when creating reducers manually. If we use
createSliceto create a slice, we can modify thestatedirectly.
It is important to note that in the case of createSlice reducers, we directly modify the state object, however, in the backend, Redux creates a new duplicate object and assigns it the new changes, so the rule is being followed, but in the backend.
¡Gracias por tus comentarios!
Pregunte a AI
Pregunte a AI
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla
Genial!
Completion tasa mejorada a 3.45
Rules for Reducers
Desliza para mostrar el menú
There are certain rules while using Redux Reducers which we need to follow to avoid certain bugs or surprises.
- Reducers should use only the
stateandactionarguments to calculate the new state. They shouldn't access any external values; - Reducers must not perform asynchronous logic or side effects such as making API calls or running timers etc.;
- The state should not be directly modified inside the reducer functions, instead, the reducer function should return a duplicate state object with updated values. This rule only applies when creating reducers manually. If we use
createSliceto create a slice, we can modify thestatedirectly.
It is important to note that in the case of createSlice reducers, we directly modify the state object, however, in the backend, Redux creates a new duplicate object and assigns it the new changes, so the rule is being followed, but in the backend.
¡Gracias por tus comentarios!