📊 We use cookies & analytics to improve your experience. Learn more
Latest

React & Modern Front-End Development Module 4 · Lesson 10 of 15

0% Exit to course

Module 4 · State that scales Lesson 10 of 15

useReducer for state with rules

Learning objective

When several pieces of state change together, useReducer keeps the rules in one place: a function that takes the current state and an action and returns the next state. It is useState with a plan.

Worked example

dispatch({ type: 'added', item }) routes to one switch that decides how the list changes, instead of five scattered setState calls.

Your practice task

Convert a component with three related useState calls into one useReducer with three actions.

Do this before moving on. Reading a solution teaches the shape of it; typing it teaches you.

Key takeaway

When several pieces of state change together, useReducer keeps the rules in one place: a function that takes the current state and an action and returns the next state. It is useState with a plan.

Create free account or sign in

Almost there

15 lessons left of 15

Finish every lesson to unlock the certificate. Each one ends in a practice task — do them and the final project brief writes itself.

Next recommended course Full-Stack Web Development Bootcamp