Understanding the Role of Reducers
Theory
Reducers are pure functions that specify how the state should change in response to dispatched actions. By taking in the current state and an action as parameters, reducers return the new state of the application.
Practice
In our project's reducers folder, we have the counterReducer.js file where we define the reducer for our counter functionality:
import { createReducer } from "@reduxjs/toolkit";
import { increment, decrement } from "../actions/counterAction.js";
const initialState = 0;
const counterReducer = createReducer(initialState,
  (builder) => {
    builder
      .addCase(increment, (state) => state + 1)
      .addCase(decrement, (state) => state - 1);
});
export default counterReducer;
Code explanation:
- Line 1: Import the 
createReducerfunction from the@reduxjs/toolkitpackage. This function is used to create reducers in Redux; - Line 2: Import the 
incrementanddecrementaction creators from the../actions/counterAction.jsfile. These action creators are used to define the actions that the reducer will handle; - Line 4: Set the 
initialStateconstant to0. This represents the initial value of the counter in the Redux store; - Line 6: Use the 
createReducerfunction to define thecounterReducer; - Line 6-11: The 
createReducerfunction takes two arguments: theinitialStateand a callback function that defines how the state should be updated based on dispatched actions; - Line 7: Use the 
builderobject to define the cases for different actions inside the callback function; - Line 9, 10: Use the 
addCasemethod of thebuilderobject to define how the state should be updated when specific actions, such asincrementanddecrement, are dispatched; - Line 9, 10: The callback function inside 
addCasetakes the current state (state) as an argument and returns the new state after applying the corresponding action; - In this case, when the 
incrementaction is dispatched, the state is incremented by1; when thedecrementaction is dispatched, the state is decremented by1; - Line 13: Finally, export the 
counterReduceras the default export of the module. 
Note
To summarize, the
counterReducerwill handle the dispatched actions and update the counter state accordingly in the Redux store.
1. What function is used to create reducers in Redux?
2. What is the purpose of the builder object in the code?
3. When the increment action is dispatched, what happens to the state?
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Can you explain how the builder callback works in createReducer?
What happens if an unknown action is dispatched to the counterReducer?
Can you show an example of how to dispatch the increment or decrement actions?
Awesome!
Completion rate improved to 4.17
Understanding the Role of Reducers
Swipe to show menu
Theory
Reducers are pure functions that specify how the state should change in response to dispatched actions. By taking in the current state and an action as parameters, reducers return the new state of the application.
Practice
In our project's reducers folder, we have the counterReducer.js file where we define the reducer for our counter functionality:
import { createReducer } from "@reduxjs/toolkit";
import { increment, decrement } from "../actions/counterAction.js";
const initialState = 0;
const counterReducer = createReducer(initialState,
  (builder) => {
    builder
      .addCase(increment, (state) => state + 1)
      .addCase(decrement, (state) => state - 1);
});
export default counterReducer;
Code explanation:
- Line 1: Import the 
createReducerfunction from the@reduxjs/toolkitpackage. This function is used to create reducers in Redux; - Line 2: Import the 
incrementanddecrementaction creators from the../actions/counterAction.jsfile. These action creators are used to define the actions that the reducer will handle; - Line 4: Set the 
initialStateconstant to0. This represents the initial value of the counter in the Redux store; - Line 6: Use the 
createReducerfunction to define thecounterReducer; - Line 6-11: The 
createReducerfunction takes two arguments: theinitialStateand a callback function that defines how the state should be updated based on dispatched actions; - Line 7: Use the 
builderobject to define the cases for different actions inside the callback function; - Line 9, 10: Use the 
addCasemethod of thebuilderobject to define how the state should be updated when specific actions, such asincrementanddecrement, are dispatched; - Line 9, 10: The callback function inside 
addCasetakes the current state (state) as an argument and returns the new state after applying the corresponding action; - In this case, when the 
incrementaction is dispatched, the state is incremented by1; when thedecrementaction is dispatched, the state is decremented by1; - Line 13: Finally, export the 
counterReduceras the default export of the module. 
Note
To summarize, the
counterReducerwill handle the dispatched actions and update the counter state accordingly in the Redux store.
1. What function is used to create reducers in Redux?
2. What is the purpose of the builder object in the code?
3. When the increment action is dispatched, what happens to the state?
Thanks for your feedback!