Challenge: Implement the Reducer
Step 3
Focus on creating a reducer to handle dispatched actions and update the goals state in the Redux store. Reducers are pure functions that specify how the state should change in response to actions.
Example
Challenge
- Open the 
goalReducer.jsfile. - Create the reducer using the 
createReducerfunction from the@reduxjs/toolkitpackage. This function simplifies the process of creating reducers. - Set the initial state of the goals to an empty array (
[]). - Inside the 
createReducerfunction, specify the cases for different actions using the actions created in thegoalAction.jsfile. - For adding a goal, use the 
.addCasemethod and pass in theaddGoalaction as the first argument. In the corresponding callback function, update the state by pushing theaction.payload(the goal) into the state array. - For removing a goal, use the 
.addCasemethod and pass in theremoveGoalaction as the first argument. In the corresponding callback function, update the state by returning a new array that filters out the goal with the matchinggoal.idfrom the state array. 
- The 
createReducerfunction simplifies the process of creating reducers by providing a convenient syntax. - Use the 
.addCasemethod of thebuilderobject to specify the cases for different actions. - Access the payload of the action using 
action.payload. 
Everything was clear?
Thanks for your feedback!
SectionΒ 3. ChapterΒ 5
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Suggested prompts:
Can you explain how the addCase method works in createReducer?
What should the remove goal case look like in the reducer?
Can you walk me through the steps to implement the goalReducer?
Awesome!
Completion rate improved to 4.17
Challenge: Implement the Reducer
Swipe to show menu
Step 3
Focus on creating a reducer to handle dispatched actions and update the goals state in the Redux store. Reducers are pure functions that specify how the state should change in response to actions.
Example
Challenge
- Open the 
goalReducer.jsfile. - Create the reducer using the 
createReducerfunction from the@reduxjs/toolkitpackage. This function simplifies the process of creating reducers. - Set the initial state of the goals to an empty array (
[]). - Inside the 
createReducerfunction, specify the cases for different actions using the actions created in thegoalAction.jsfile. - For adding a goal, use the 
.addCasemethod and pass in theaddGoalaction as the first argument. In the corresponding callback function, update the state by pushing theaction.payload(the goal) into the state array. - For removing a goal, use the 
.addCasemethod and pass in theremoveGoalaction as the first argument. In the corresponding callback function, update the state by returning a new array that filters out the goal with the matchinggoal.idfrom the state array. 
- The 
createReducerfunction simplifies the process of creating reducers by providing a convenient syntax. - Use the 
.addCasemethod of thebuilderobject to specify the cases for different actions. - Access the payload of the action using 
action.payload. 
Everything was clear?
Thanks for your feedback!
SectionΒ 3. ChapterΒ 5