useState
useState is one of the foundational pieces of modern React engineering.
Introduction
useState is one of the foundational pieces of modern React engineering. In plain English: it deals with local component state.
Beginner analogy: think of React like LEGO — small, reusable bricks (components) snap together to form bigger structures (pages). useState is one specific brick (or rule about how bricks connect) that you'll reach for in almost every real project, from a personal portfolio to a production SaaS dashboard used by millions.
By the end of this lesson you will understand the concept, see a working example, learn the most common mistakes, and be ready to answer interview questions about it.
Understanding the topic
Core concepts:
- useState — local component state.
- How it fits into the React rendering lifecycle.
- Practical syntax with a tiny, copy-pasteable example.
- Common mistakes and how to avoid them in real apps.
- Production-grade pattern used by senior frontend engineers.
- Performance and accessibility considerations.
- Where this concept appears in real SaaS dashboards.
Syntax reference
Visual workflow / architecture:
User Interaction│▼React Event Handler│▼setState / Hook Update│▼┌──────────────────┐│ Virtual DOM │ build new VDOM tree└──────────────────┘│▼┌──────────────────┐│ Reconciliation │ diff old vs new└──────────────────┘│▼Efficient DOM Patch│▼Updated UI on Screen
Real-world use
In real frontend teams, useState shows up everywhere — from Stripe Dashboard filters, to Linear issue lists, to Vercel deployment cards. Engineers rely on local component state to keep UIs fast, predictable, and easy to evolve. Use it whenever you need clear data flow and a testable surface.
Best practices
- Keep useState usage small, focused, and named clearly.
- Co-locate state with the component that owns it; lift only when needed.
- Prefer composition over configuration — props in, JSX out.
- Always add types (TypeScript) to props and hooks.
- Profile before optimizing — measure with React DevTools Profiler.
Common mistakes
- Using useState inside conditions or loops — breaks hook ordering.
- Skipping dependency arrays — leads to stale closures and bugs.
- Mutating state directly instead of returning a new object/array.
- Forgetting cleanup in useEffect — causes memory leaks.
Hands-on exercise
Interview preparation — practice these questions:
- Q1. What is useState and when would you use it?
- Q2. How does useState differ from related React concepts?
- Q3. Walk through a real example of useState in a SaaS dashboard.
- Q4. What are the common bugs around useState and how do you debug them?
- Q5. Scenario: a teammate ships useState that re-renders on every keystroke — how do you fix it?