Lesson 6 of 15
Managing Multiple States
Lesson Updated: Apr 28, 2026
Requirement
Learn how to manage multiple pieces of state in a single component without making a mess.
Assignments (Start First)
- Simple beginner UI task: Create a
UserFormcomponent with two separate state variables: one forfirstNameand one forlastName. Render two inputs and display the full name below. - Slightly improved version: Add a third state variable for
age(a number). Add a button that increments the age by 1. - Practical real-world component: Build a
SettingsPanelcomponent. Use three different states to track: a volume slider (number), a "notifications enabled" toggle (boolean), and a "theme" selector (string dropdown). - "The Escalation": Create a
RegistrationFormthat uses a single state object{ email: "", password: "", username: "" }instead of three separate state variables. Update the object correctly without deleting the other fields.
Lessons (Optional)
- Multiple useStates: You can call
useStateas many times as you need at the top of your component. - State Objects: For related data (like form fields), you can store an object in state, but remember to use the spread operator (
...prev) to copy existing fields when updating.
The Summary and Quizzes
- Concept explanation: Complex UIs often track many things at once (text, toggles, numbers). You can split these into separate
useStatehooks or group them into state objects. - Why it works in React: React keeps track of every
useStatecall in the exact order they are written, managing their distinct values across re-renders. - Challenge question: If you have
const [user, setUser] = useState({ name: "Bob", age: 30 }), how do you update just the age to 31 without losing the name?