State
Managing component state with useState, re-renders, and the difference between state and props.
What you'll learn
- What state is and how it differs from props
- Using the useState hook to add state to function components
- How state updates trigger re-renders
- Functional updates and the batched update behavior
Concept
What Is State?
While props are external inputs passed from a parent, state is internal data that a component owns and manages. State is what makes a component interactive — it can change over time in response to user actions, network responses, or timers. When state changes, React automatically re-renders the component to reflect the new state. This is the core of React's reactive model: UI = f(state).
The useState Hook
The useState hook is the primary way to add state to a function component. You call it with an initial value, and it returns a pair: the current state value and a function to update it. By convention, you destructure it as const [count, setCount] = useState(0). The initial value is only used on the first render; subsequent renders ignore it. You can pass any type as the initial value: a number, string, boolean, array, or object.
Re-renders and Batching
When you call a state setter, React schedules a re-render. React batches multiple state updates within the same event handler into a single re-render for performance. This means calling setCount(1) and setCount(2) in the same handler results in one render with the final value. In React 18, batching is automatic even in promises, timeouts, and native event handlers.
Functional Updates
Because state updates can be asynchronous and batched, you should never compute the next state based on the current state variable directly. Instead, use the functional update form: setCount(prev => prev + 1). This guarantees you are working with the most recent state. This is especially important when multiple updates happen in the same handler or when the state depends on its previous value.
State for Objects and Arrays
When state holds an object or array, you must create a new reference when updating — never mutate. For objects, use the spread operator: setUser({ ...user, name: "New" }). For arrays, use methods that return new arrays: setItems([...items, newItem]) or setItems(items.filter(...)). React compares references to detect changes, so mutation without a new reference will not trigger a re-render.
Example
import { useState } from "react";
function Counter() {
const [count, setCount] = useState(0);
const [step, setStep] = useState(1);
const increment = () => {
// Functional update — always uses the latest state.
setCount(prev => prev + step);
};
const reset = () => {
setCount(0);
setStep(1);
};
return (
<div className="counter">
<p>Count: {count}</p>
<label>Step: <input type="number" value={step}
onChange={e => setStep(Number(e.target.value))} /></label>
<button onClick={increment}>+{step}</button>
<button onClick={reset}>Reset</button>
</div>
);
}
useState returns [value, setter]. The functional update form setCount(prev => prev + step) ensures correctness when multiple updates batch together.
Try it
- JSON Formatter
State objects can be complex — format them to inspect the structure.
- Code Diff
Compare state shapes before and after a refactor.
Common mistakes
The mistake
Using the state variable directly instead of the functional update form
The fix
When the next state depends on the previous state, use setCount(prev => prev + 1), not setCount(count + 1). The functional form always uses the latest value.
The mistake
Mutating state objects directly instead of creating new ones
The fix
Never do state.items.push(newItem). Instead, create a new array: setItems([...items, newItem]). React compares references to detect changes — mutations are invisible to it.
Practice State
17 exercises
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.