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.

🛒
UrbanCart Example
State is everywhere on the product page

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.

How State Drives the UI
User Action
Click "Add to Cart"
→
State Updates
count: 0 → 1
→
Re-render
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.

useState-basics.jsx The Pattern
📦

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.

Rule: Always call 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.

update-state.jsx Right vs Wrong
Why immutable? React compares the old state with the new state to decide whether to re-render. If you mutate the existing value, React thinks nothing changed — the reference is the same. Always create a new value.

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

object-state.jsx Spread to Update Objects

Array State — Building a Cart

array-state.jsx Spread to Update Arrays

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.

multiple-state.jsx Separate Concerns
Guideline: If two values always change together, combine them into one object. If they change independently (like 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
state-vs-props.jsx Props from Parent, State Inside

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.

Lifting State Up Pattern
Parent
owns cartCount state
↙ ↘
ProductCard
calls onAdd prop
CartSummary
reads cartCount prop
lifting-state.jsx Shared State via Parent
When to lift state: If Component A needs to change something that Component B displays, move that state to their common parent. The parent passes the value down as a prop and the setter as a callback.

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 — Shopping Cart Counter
Output

Try It Yourself #2

Build a product listing with toggleable favorite hearts. Track how many products are favorited using a computed value from state.

🛠️ Try It Yourself — Toggle Favorites
Output

Test your knowledge

Question 1 of 5
What does useState return?
Question 2 of 5
Why should you never mutate state directly (e.g., count++)?
Question 3 of 5
How do you correctly update an object in state?
Question 4 of 5
What is the key difference between state and props?
Question 5 of 5
When should you "lift state up" to a parent component?
0
/5
Great job!

Up Next

Lesson 16: Event Handling in React →
You've mastered state! Next: handle clicks, inputs, and forms like a pro.
Africa & India's Best AI Academy
🤖 AI-first 🎓 Placement Assistance

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 →