Question: We are going to refactor our custom useApplicationData Hook. We will move from useState to useReducer. The reducer will return a new state object each
We are going to refactor our custom useApplicationData Hook. We will move from useState to useReducer. The reducer will return a new state object each time it handles a dispatched action. You're already familiar with this hook as we used it during Fancy Buttons and learned about passing down data with props during Tourney Matches. Consider how you will implement useReducer to track when a photo is favourited and unfavourited by the user. Creating The Reducer The reducer will handle different actions (view the code below). Instruction Remove useState from useApplicationData and replace it with useReducer. The logic used to store photos, store topics, favourite photos, selected photo shouldn't need to change. So no logic changes. We just need to move it to a new location within the reducer. /* insert app levels actions below */ export const ACTIONS = { FAV_PHOTO_ADDED: 'FAV_PHOTO_ADDED', FAV_PHOTO_REMOVED: 'FAV_PHOTO_REMOVED', SET_PHOTO_DATA: 'SET_PHOTO_DATA', SET_TOPIC_DATA: 'SET_TOPIC_DATA', SELECT_PHOTO: 'SELECT_PHOTO', DISPLAY_PHOTO_DETAILS: 'DISPLAY_PHOTO_DETAILS' } function reducer(state, action) { switch (action.type) { case FAV_PHOTO_ADDED: return { /* insert logic */ } { /* insert all relevant actions as case statements*/ } } default: throw new Error( `Tried to reduce with unsupported action type: ${action.type}` ); } } In the Reducers exercise
Step by Step Solution
There are 3 Steps involved in it
To implement useReducer in refactoring the custom useApplicationData Hook in order to track when a photo is favorited or unfavorited by the user we ne... View full answer
Get step-by-step solutions from verified subject matter experts
