Where is component state stored?
1. At the code level
When you write, for example:
function Counter() {
const [count, setCount] = useState(0);
return <button onClick={() => setCount(count + 1)}>{count}</button>;
}It seems as if count is stored simply in a variable inside the function.
But this is not the case, because the function is called again on every render,
which means all local variables inside it are reset.
2. Where state is actually stored
In fact, the state is stored inside React itself, not in the component directly.
During render, React creates an internal structure (Fiber node) for each component, where it keeps all the internal-service information:
- the current state (
state) - hooks (
useState,useEffect,useRef, etc.) - references to child components
- previous props and state
- flags for updates, and so on
That is, useState does not create a variable in your function,
it registers it inside React Fiber.
3. What happens on setState / setCount
When you call setCount(newValue):
- React saves the new state value in its internal structure (Fiber).
- Marks the component as "needing an update".
- Re-renders the component (will call the
Counter()function again). - On the new render, React retrieves the saved state and substitutes it into
[count, setCount].
So the value of
count"remembers" its past, even though theCounterfunction is called again and again.
4. Where exactly it is stored (simplified)
If you picture it as a structure:
FiberNode = {
type: Counter,
stateHooks: [
{ state: 0, queue: [updateFn1, updateFn2, ...] }
],
props: {},
memoizedState: ...,
...
}Then the state is stored in stateHooks or memoizedState of the Fiber object,
not in your function directly.
5. Summary:
| Question | Answer |
|---|---|
| Where is the state stored? | Inside React (in the Fiber structure associated with the component). |
| Why not in the function itself? | Because a component is just a function that gets called every render. |
| How does React "remember" past state? | It stores it between function calls, in its internal component tree. |
Short Answer
Interview readyA concise answer to help you respond confidently on this topic during an interview.