Reducer
Step 4: Reducers and their role in state management
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.
Complete code
Bedankt voor je feedback!
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
Awesome!
Completion rate improved to 1.96
Reducer
Veeg om het menu te tonen
Step 4: Reducers and their role in state management
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.
Complete code
Bedankt voor je feedback!