 Challenge: Reducer
Challenge: Reducer
Step 3: Create reducer
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.
Tout était clair ?
Merci pour vos commentaires !
Section 3. Chapitre 11
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion
Awesome!
Completion rate improved to 1.96 Challenge: Reducer
Challenge: Reducer
Glissez pour afficher le menu
Step 3: Create reducer
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.
Tout était clair ?
Merci pour vos commentaires !
Section 3. Chapitre 11