useReducer
Part of the React course
A checkout form starts simple. There is a value, so you add useState. Then it needs an error message, so that is another useState. Then a loading flag, then a "has the user touched this field yet" flag, and now four separate pieces of state have to be updated in the right combination by six different event handlers. Submitting has to set loading true, clear the error, and disable the button.
useReducer is React's answer to that. Instead of scattering the update rules across handlers, you write one function that describes every way the state is allowed to change, and components request a change by name. The mental image is a vending machine. The machine holds all the state, its wiring decides what each button does, and you interact with it by pressing a labelled button rather than by reaching inside and...
Want to learn React useReducer in a real environment?
Click below, get a fresh Linux box with React ready to go, and work through useReducer hands-on with an AI tutor that knows your code.
Start this lessonuseReducerWhat you'll learn
- When to reach for useReducer
- Reducer function shape
- Action objects
- Dispatch
- Combining useReducer with useContext
Read this lesson in your language
All lessons in this course
17 lessons- 1What is React?25 min
- 2JSX and Components30 min
- 3Props30 min
- 4State with useState35 min
- 5Handling Events30 min
- 6Conditional Rendering and Lists30 min
- 7Forms and Controlled Inputs35 min
- 8Effects with useEffect40 min
- 9Data Fetching40 min
- 10Component Composition35 min
- 11Context and useContext35 min
- 12useReducer35 min
- 13Custom Hooks35 min
- 14Performance and Memoization35 min
- 15Routing with React Router40 min
- 16Testing React Components40 min
- 17Capstone: Todo App60 min
