useState
In one line
useState is a Hook that lets a function component hold and update local state, returning the current value and a setter function.
In simple words
useState is the most commonly used Hook. It declares a piece of state inside a function component. You call it with an initial value, and it returns an array with two elements: the current state value and a function to update it.
const [count, setCount] = useState(0) — here count starts at 0, and setCount is the updater. When you call setCount(5), React schedules a re-render with the new value.
Important behaviors: the setter can take a new value or an updater function setCount(prev => prev + 1) (useful when the new state depends on the old). State updates are asynchronous — calling the setter does not immediately change the variable. Also, unlike this.state in class components, useState does not merge update objects automatically; you must spread previous state yourself when updating objects.
Example
function Toggle() {
const [on, setOn] = useState(false);
return (
<button onClick={() => setOn(!on)}>
{on ? "ON" : "OFF"}
</button>
);
}on starts as false. Each click flips it. React re-renders the button with the new label after each update.
Common confusions
Confused with: useReducer
The difference: useState is for simple independent values. useReducer is for complex state logic involving multiple sub-values or when the next state depends on the previous one in non-trivial ways.
Related terms
Related Resources
Related Lessons
- State
Learn useState in depth.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.