1. What is State
State is data that changes over time. In a static HTML page, everything is fixed — but in a React app, things are alive. A cart count goes from 0 to 3. A search query changes with every keystroke. A product gets marked as a favorite.
When state changes, React automatically re-renders the component to show the new data. You never manually touch the DOM — you update state, and React handles the rest.
When a customer browses UrbanCart, state powers every interaction. The cart count in the header updates when they add an item. The selected color highlights when clicked. The search query filters products in real time. The favorite heart toggles red. All of these are state — data that changes, triggering a re-render.
Click "Add to Cart"
count: 0 → 1
UI shows new count
2. The useState Hook
The useState hook is how you create state in a React function component. It returns an array with two items: the current value and a setter function to update it.
count
The current state value. Starts at whatever you pass to useState() — here, 0. Read it anywhere in your component.
setCount
The setter function. Call it with a new value to update state and trigger a re-render. Never modify count directly.
useState at the top level of your component — never inside loops, conditions, or nested functions. React relies on the order of hook calls to track state correctly.
3. Updating State
To update state, call the setter function with the new value. State is immutable — you never change it directly. Instead, you create a new value and pass it to the setter.
4. State with Objects & Arrays
When state is an object or array, you must use the spread operator to create a new copy. Never mutate the original.
Object State — Updating a Product
Array State — Building a Cart
5. Multiple State Variables
You can call useState as many times as you need. Keep separate concerns in separate state variables — don't cram everything into one giant object.
query and sortAsc above), keep them separate.
6. State vs Props
Both state and props hold data — but they work very differently. Understanding the distinction is key to thinking in React.
| State | Props | |
|---|---|---|
| Owned by | The component itself | The parent component |
| Mutable? | Yes — via setter function | No — read-only |
| Triggers re-render? | Yes, when updated | Yes, when parent passes new values |
| Declared with | useState() |
Function parameters |
| UrbanCart example | Cart count, search query | Product name, price passed to card |
7. Lifting State Up
When two sibling components need to share state, move the state up to their closest common parent. The parent owns the state and passes it down as props.
owns cartCount state
calls onAdd prop
reads cartCount prop
Try It Yourself #1
Build an UrbanCart shopping cart counter with + and - buttons and a quantity selector. Watch how useState keeps the UI in sync with every click.
Try It Yourself #2
Build a product listing with toggleable favorite hearts. Track how many products are favorited using a computed value from state.
Test your knowledge
useState return?count++)?You just made React come alive with state.
Now build a full-stack career.
Join the Best AI Programs — three 5-month courses, fully online, with placement assistance. Week 1 is free.
Explore our AI-era courses →