useState
The React hook that adds a state variable to a component and returns the current value with a function to change it.
In more detail
const [count, setCount] = useState(0) gives you the value for this render and a setter. Calling the setter does not change count immediately; it asks React to render again with the new value. Passing a function to the setter, as in setCount((prev) => prev + 1), always works from the latest queued value.
Examples
const [open, setOpen] = useState(false);const [count, setCount] = useState(0);Related terms
- React hook — A function whose name starts with use, such as useState or useEffect, that lets a React component keep state or run side work.
- Client Component — A component whose code is also sent to the browser so it can use state, effects and event handlers.
- useReducer — The React hook that manages state through a reducer function, returning the current state and a dispatch function.
- component state — Data a component remembers between renders, such as typed text or whether a menu is open; changing it makes React render again.
- updater function — A function passed to a state setter that receives the latest pending state and returns the next state.
- automatic batching — React groups several state updates made in the same event or task into a single re-render.
- state snapshot — The idea that during one render every state value is fixed, so code in that render always sees the same value even after calling a setter.
Taught in
- Server Components: Async Execution & Constraints — Next.js 15 & Modern App Router Architecture
- Client Components: use client & Hydration Models — Next.js 15 & Modern App Router Architecture
- Root & Nested Layouts: State Preservation Semantics — Next.js 15 & Modern App Router Architecture
- Templates vs Layouts: Remounting & Page Transitions — Next.js 15 & Modern App Router Architecture
- Optimistic UI: useOptimistic Rollbacks & Transitions — Next.js 15 & Modern App Router Architecture
- Props Contract: Immutability, Children, And Polymorphic Tags — React 19 & Modern Component Architecture
- State with useState: Immutability And Asynchronous Batching — React 19 & Modern Component Architecture
- Complex State with useReducer: State Machines And Action Types — React 19 & Modern Component Architecture
- Keys In Depth: Identity Preservation And Identity Resetting — React 19 & Modern Component Architecture
- Diffing Algorithm: Element Type Shifts And Subtree Tear Down — React 19 & Modern Component Architecture
- Avoiding Effect Antipatterns: Derived State And Events — React 19 & Modern Component Architecture
- useRef Mechanics: DOM References And Mutable Persistent State — React 19 & Modern Component Architecture
- Lifting State Up: Colocation, Ownership, And Data Flow — React 19 & Modern Component Architecture
- useContext in Depth: Scoping, Consumer Subscription Costs — React 19 & Modern Component Architecture
- Compound Components: Inversion of Control with Context — React 19 & Modern Component Architecture