Syntax Video
Clean Up React Code with useReducer | Code Renovation
In this video CJ shows how to refactor React code that has several useStates into a single useReducer. He talks about what useReducer is, shows how to create a reducer from the ground up and talks about when you should and should not reach for useReducer.
- 0:00 intro
- 1:12 code setup
- 2:05 when should we use a reducer?
- 2:52 convert a single useState to a useReducer
- 3:26 create a reducer function
- 3:38 create a state type for our reducer
- 4:47 a simple reducer
- 5:08 defining action types
- 6:32 updating reducer state with actions
- 7:46 review: what is a reducer?
- 8:13 use a reducer with useReducer
- 9:24 updating types and dispatching actions
- 11:13 when to use a reducer
- 11:21 refactor options state into reducer
- 14:38 when to use a reducer
- 14:59 continue refactoring options state
- 17:01 when to use useReducer
- 17:53 moving fetch state into reducer
- 19:15 finding and grouping actions
- 19:56 defining grouped actions
- 20:27 replacing setState with dispatch actions
- 21:41 when to define actions with no payload
- 22:29 combining action types with no payload
- 23:43 review usage of reducer and dispatch
- 24:04 other action type options
- 24:31 tradeoffs to choosing useReducer
- 25:02 benefits of useReducer
- 26:15 thanks!