Challenge: Connect Redux with React
Step 4
Focus on involving Redux logic in a React app. Connect React components and the Redux store to enable state management and actions.
Example
Ship Tracker App
Challenge
Form.jsx:
- Open the
Form.jsxfile. - Import the
useDispatchhook from thereact-reduxpackage. This hook will be used to dispatch actions to the Redux store. - Import the
addGoalaction from thegoalAction.jsfile. This action is responsible for adding a goal to the Redux store. - Initialize the
dispatchvariable inside the component by calling theuseDispatchhook. - Complete the
handleFormSubmitfunction by dispatching theaddGoalaction. Use thedispatchfunction and pass in an object with the goal details:{ id: Date.now(), text: goal }. TheDate.now()generates a unique ID for each goal. - Reset the form after submission by calling the
resetFormfunction.
GoalList.jsx:
- Open the
GoalList.jsxfile. - Import the
useDispatchanduseSelectorhooks from thereact-reduxpackage. These hooks will be used to access the Redux store and dispatch actions. - Import the
removeGoalaction from thegoalAction.jsfile. This action is responsible for removing a goal from the Redux store. - Initialize the
goalsvariable using theuseSelectorhook and access thegoalsstate from the Redux store. - Initialize the
dispatchvariable by calling theuseDispatchhook. - Complete the
handleRemoveGoalfunction by dispatching theremoveGoalaction. Pass in thegoalas a parameter. - Finish the logic of rendering the array of goals by mapping over the
goalsarray and rendering each goal. - Display the text of each goal and add a button with an
onClickevent that calls thehandleRemoveGoalfunction and passes in the respectivegoal.
- Use theΒ
useDispatchΒ andΒuseSelectorΒ hooks to access the Redux store and dispatch actions. - Work with theΒ
removeGoalΒ andΒaddGoalΒ actions fromΒgoalAction.jsΒ to handle actions related to goals. - Ensure the payload of theΒ
addGoalΒ action includes a uniqueΒidΒ generated usingΒDate.now()Β and the text value from the input field. - Implement theΒ
handleRemoveGoalΒ function to dispatch theΒremoveGoalΒ action with the corresponding goal. - Utilize theΒ
mapΒ function to iterate over theΒgoalsΒ array and render each goal, displaying its text.
Everything was clear?
Thanks for your feedback!
SectionΒ 3. ChapterΒ 6
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Suggested prompts:
Can you explain how to use the useDispatch and useSelector hooks in this context?
What should I do if my addGoal or removeGoal actions aren't working as expected?
Can you walk me through the process of connecting a React component to the Redux store?
Awesome!
Completion rate improved to 4.17
Challenge: Connect Redux with React
Swipe to show menu
Step 4
Focus on involving Redux logic in a React app. Connect React components and the Redux store to enable state management and actions.
Example
Ship Tracker App
Challenge
Form.jsx:
- Open the
Form.jsxfile. - Import the
useDispatchhook from thereact-reduxpackage. This hook will be used to dispatch actions to the Redux store. - Import the
addGoalaction from thegoalAction.jsfile. This action is responsible for adding a goal to the Redux store. - Initialize the
dispatchvariable inside the component by calling theuseDispatchhook. - Complete the
handleFormSubmitfunction by dispatching theaddGoalaction. Use thedispatchfunction and pass in an object with the goal details:{ id: Date.now(), text: goal }. TheDate.now()generates a unique ID for each goal. - Reset the form after submission by calling the
resetFormfunction.
GoalList.jsx:
- Open the
GoalList.jsxfile. - Import the
useDispatchanduseSelectorhooks from thereact-reduxpackage. These hooks will be used to access the Redux store and dispatch actions. - Import the
removeGoalaction from thegoalAction.jsfile. This action is responsible for removing a goal from the Redux store. - Initialize the
goalsvariable using theuseSelectorhook and access thegoalsstate from the Redux store. - Initialize the
dispatchvariable by calling theuseDispatchhook. - Complete the
handleRemoveGoalfunction by dispatching theremoveGoalaction. Pass in thegoalas a parameter. - Finish the logic of rendering the array of goals by mapping over the
goalsarray and rendering each goal. - Display the text of each goal and add a button with an
onClickevent that calls thehandleRemoveGoalfunction and passes in the respectivegoal.
- Use theΒ
useDispatchΒ andΒuseSelectorΒ hooks to access the Redux store and dispatch actions. - Work with theΒ
removeGoalΒ andΒaddGoalΒ actions fromΒgoalAction.jsΒ to handle actions related to goals. - Ensure the payload of theΒ
addGoalΒ action includes a uniqueΒidΒ generated usingΒDate.now()Β and the text value from the input field. - Implement theΒ
handleRemoveGoalΒ function to dispatch theΒremoveGoalΒ action with the corresponding goal. - Utilize theΒ
mapΒ function to iterate over theΒgoalsΒ array and render each goal, displaying its text.
Everything was clear?
Thanks for your feedback!
SectionΒ 3. ChapterΒ 6