Short answer
useState is the React Hook that adds a state variable to a component and returns the current value plus a function that updates it. You call it at the top level, not inside a loop or a condition. The set function changes the value for the next render. Reading the variable after you call set still shows the old value.
Last verified against the sources listed below.
Key takeaways
- Call useState at the top of the component. It returns a two-item array.
- The first item is the current state. The second is the set function.
- A function passed as the first argument is an initializer, and it must be pure.
- The set function updates the next render. React batches those updates.
- Replace objects and arrays in state. Mutating them can make React ignore the update.
A direct answer to what is usestate is the React Hook that stores a value on a component. The useState reference says it adds a state variable and returns the current value with a function that can change it.
You write the call at the top level of the component. The conventional line puts the two return values into two names:
const [count, setCount] = useState(0);
The first name is the value on this render. The second name is the set function. Calling it asks React to render again with a new value.
What does the hook return?
It returns an array of exactly two items. During the first render, the state matches the initialState you passed. The set function lets you update that state to a different value and trigger a re-render.
| Piece | Role in the reference |
|---|---|
| initialState | The starting value. Ignored after the first render. |
| Current state | Matches initialState on the first render. |
| Set function | Updates state for the next render. It has no return value. |
The names are a convention, not a requirement. React’s page shows pairs such as age and setAge, or name and setName. Array destructuring is the syntax that unpacks the two-item array into those names. The guide says the array itself is not modified.
initialState can be any type. It is ignored after the initial render. If you pass a function, React treats it as an initializer.
That function should be pure, take no arguments, and return the first value. React calls it while initializing the component and stores the return value.
Where are you allowed to call it?
You may call it only at the top level of a component or of your own Hook. The reference says you cannot call it inside loops or conditions. If you need that, extract a new component and move the state there.
In Strict Mode, React calls the initializer twice in development so impure setup is easier to spot. That extra call does not run in production. If the function is pure, the extra call does not change the result, and one of the two results is ignored.
JavaScript is the language the call is written in.
JavaScript vs React is the split between that language and the UI library that defines this Hook. useState is library API, not a JavaScript keyword.
What does the set function do?
The set function schedules the next value and a re-render. You can pass the next state directly, or pass a function that calculates it from the pending state.
setCount(count + 1);
setCount(function (value) { return value + 1; });
If you pass a function, React treats it as an updater. It must be pure, it should take the pending state as its only argument, and it should return the next state. React queues those functions and applies them on the next render.
The set function only updates the variable for the next render. If you read the state variable after calling set, you still get the old value from before the call. Set functions do not return a value. The function’s identity is stable, so leaving it out of an Effect dependency list is a pattern the reference describes.
React batches state updates. It updates the screen after all the event handlers have run and have called their set functions. That prevents multiple re-renders during a single event. The reference points at flushSync only for the rare case where you must update the screen earlier, such as when you need to read the DOM.
What if the next value matches?
React skips the re-render when the next value is the same as the current one. The comparison is Object.is. MDN says Object.is checks whether two values are the same value, and that it has been widely available since September 2015.
That comparison is not the same as === for every input. MDN says Object.is treats NaN as the same as NaN, and it treats positive zero and negative zero as different. React uses that test for the skip.
State is read-only. If you store an object or an array, replace it instead of mutating it. Changing a property and passing the same object back makes Object.is succeed, so React ignores the update. The fix is a new object or a new array.
In Strict Mode, updater functions also run twice in development. An impure updater that pushes into an existing array can show the change twice, which is how the double call exposes the mistake. A pure updater that returns a new array does not change behavior when it runs again.
How does this sit on a page?
The value you store is what the component shows the next time it renders. The HTML Living Standard says the specification provides scripting APIs for pages that range from static documents to dynamic applications. A state variable is one way a React component keeps a dynamic value between those renders.
Passing a function as state is a special case. React will treat a function argument as an initializer or an updater and call it. To store a function, wrap it so the argument you pass is a function that returns your function. The reference shows that wrapper as an arrow that returns the function you meant to keep.
The SVG to JSX tool turns an SVG into a React component. That component can call useState if the graphic needs a value that changes, such as a selected state. The tool does not add the Hook for you. It only gives you a component where a top-level useState call is legal.
Frequently asked questions
Why does the hook return an array?
The React reference says useState returns an array with exactly two values: the current state, and the set function that updates it and triggers a re-render. The usual names are something and setSomething, taken with array destructuring. The JavaScript.info guide describes that syntax as unpacking an array into variables, and it says the array itself is not modified.
Can you call useState inside an if statement?
No. The reference says useState is a Hook, so you call it at the top level of your component or of your own Hooks. You cannot call it inside loops or conditions. If you need state that exists only in one branch, extract a new component and move the state into that component.
Why does the screen still show the old value after set?
The set function only updates the state variable for the next render. If you read the variable after calling set, React says you still get the old value that was on the screen before the call. React also batches updates. It updates the screen after all the event handlers have run and have called their set functions, which avoids extra re-renders during a single event.
What happens if you change an object in state directly?
React treats state as read-only, so you replace objects and arrays instead of mutating them. The reference says React ignores an update when the next state is the same as the current state by an Object.is comparison. If you mutate an object and pass that same object back, the update is ignored. Build a new object or array and pass that.
Why does an initializer run twice in development?
In Strict Mode, React calls your initializer function twice, and it calls an updater function twice, to help you find accidental impurities. The reference says this is development-only and does not affect production. One of the two results is ignored. The function should be pure, with no arguments for an initializer. An updater receives the pending state and returns the next state. Event handlers are not called twice.
Sources
- useState · React, 2026
- Object.is() · MDN, 2026
- Destructuring assignment · javascript.info, 2026
- HTML Living Standard · WHATWG, 2026