Why let count = 0 Breaks Completely
Exploring why let count = 0 breaks completely and common implementation patterns
1. The Problem
You know React re-renders a component when state changes. You know useState is how you declare state. But the question nobody stops to answer precisely is: why? Why can’t a plain variable do the same job? JavaScript variables change. You can increment them. You can read them. Why does React need a special API for something that is, at its core, just storing a number?
The answer is not “because that’s how React works.” The answer is mechanical. There are three separate reasons plain variables fail to drive UI updates, each one a distinct breakdown in a different part of the system. Understanding all three is what separates a developer who follows the rules from one who understands why the rules exist.
2. The Naive Attempt
Build a counter. No framework. No useState. Just a component function, a variable, and a button.
function Counter() {
let count = 0;
function increment() {
count = count + 1;
console.log('count is now:', count); // logs 1, 2, 3...
}
return (
<div>
<p>Count: {count}</p>
<button onClick={increment}>+</button>
</div>
);
}
Click the button. The console logs increment correctly: 1, 2, 3. The variable is changing. But the screen stays at 0 forever.
Now try the obvious fix: call some kind of render function manually.
let count = 0; // move it outside so it survives across renders
function Counter() {
function increment() {
count = count + 1;
// manually trigger a re-render somehow...
// but how? there's no React.render() to call here
// we don't have access to the root, or the component's position in the tree
// we don't know what to re-render
renderApp(); // hypothetical
}
return (
<div>
<p>Count: {count}</p>
<button onClick={increment}>+</button>
</div>
);
}
function renderApp() {
ReactDOM.render(<Counter />, document.getElementById('root'));
}
renderApp();
Move count outside the function so it persists. Manually call renderApp() to force a re-render. This actually works, technically. Click the button, count increments on screen.
But you have just implemented a broken version of global state with manual re-rendering. Scale it to a real app. You have 50 components. Which one do you call renderApp() from? You’d have to re-render the entire application from the root for every change in any component, because you have no way to re-render just the component that changed.
And there is a deeper problem. Move count back inside the function to see it:
function Counter() {
let count = 0; // inside the function
function increment() {
count = count + 1;
console.log(count); // 1
renderApp(); // triggers re-render
}
return (
<div>
<p>{count}</p>
<button onClick={increment}>+</button>
</div>
);
}
You click the button. increment runs. count becomes 1. renderApp() triggers a re-render. React calls Counter() again. The very first line executes: let count = 0. Count is reset to zero. The screen shows 0. You incremented it to 1 and then immediately destroyed it.
3. Why That Breaks
Three distinct failure modes. Each one is a separate mechanical problem.
Failure mode 1: React has no signal to re-render.
React does not observe your variables. It has no proxy, no getter, no mutation watcher on plain JavaScript variables. When you write count = count + 1, this is a direct memory write. The JavaScript engine updates the value in the variable binding. React is not notified. Nothing in React’s internals knows this happened. No Fiber is marked dirty. No re-render is scheduled. The screen does not update because React never learned that anything changed.
useState’s setter function is how you send that signal. Calling setCount(1) does two things: stores the new value in the Fiber’s state slot, and marks the Fiber as having pending work, scheduling a re-render through React’s scheduler. A plain assignment does neither.
Failure mode 2: Local variables are destroyed on every render.
A component function is called by React on every render. Every time it is called, it starts executing from the top. Every let, const, and var declared inside the function is created fresh. There is no memory between calls. The function has no knowledge of its previous execution.
function Counter() {
let count = 0; // created fresh every call
// by the time increment runs and triggers a re-render,
// the new call to Counter() will create a new `count` at 0
// the incremented value is unreachable
}
This is not a React behavior. This is how JavaScript functions work. A function’s local variables live on the call stack. When the function returns, the call stack frame is popped. The variables cease to exist. The next call creates new ones.
useState solves this by storing the value outside the function, on the Fiber node. The Fiber node persists for the lifetime of the component. When React calls Counter() again, the useState call does not use its initializer argument. It reads the current value from the Fiber’s memoizedState linked list. The value survives between calls because it lives in a different memory location than the function’s local scope.
Failure mode 3: Even with an external variable, the signal problem remains.
Moving count outside the function solves the survival problem but not the signal problem. React still has no mechanism to observe the mutation. You have to manually trigger re-renders, and doing so from inside a component gives you no information about which component to re-render or where it is in the tree. You either re-render everything (expensive and architecturally broken) or build your own notification system (which is what React’s state system already is).
4. React’s Mental Model
A component function is called by React. React is the caller, not the browser, not you. As the caller, React controls when the function runs and what happens after it returns. useState is a contract between your component and React: “I am declaring that this value is part of my render output. When it changes, call me again.” The setter function is how you fulfill your end of the contract: “this value changed, please call me again.”
A plain variable is outside that contract. React cannot observe it, cannot know when it changes, and cannot call your function again in response.
One sentence: useState is a mechanism for a component to ask React to re-run the component function when a specific piece of data changes, with React holding the data between runs so it survives the function returning.
5. React’s Solution
useState solves all three failure modes simultaneously.
function Counter() {
const [count, setCount] = React.useState(0);
// count: the current value, read from the Fiber's memoizedState
// setCount: the signal mechanism that marks the Fiber dirty and schedules a re-render
// 0: the initializer, used ONLY on the first render, ignored on all subsequent ones
function increment() {
setCount(count + 1);
// 1. the new value (count + 1) is enqueued on the Fiber's update queue
// 2. the Fiber is marked with pending work
// 3. React's scheduler queues a re-render
// 4. React calls Counter() again
// 5. useState reads the new value from memoizedState (not from the initializer)
// 6. count is now 1, JSX re-evaluates, screen updates
}
return (
<div>
<p>Count: {count}</p>
<button onClick={increment}>+</button>
</div>
);
}
Failure mode 1 solved: setCount is the explicit signal. React is notified.
Failure mode 2 solved: the value lives on the Fiber, survives function return.
Failure mode 3 solved: React knows exactly which Fiber to re-render because setCount is bound to a specific Fiber’s update queue.
The initializer 0 is used exactly once: the first time React creates the Fiber for this component. On every subsequent render, React reads from memoizedState. The initializer is irrelevant after mount. This is why storing a prop as state only captures the initial prop value and diverges afterward.
6. Internals
Trace what happens at the Fiber level from the moment Counter mounts to the moment you click the button.
Mount (first render):
1. React creates a Fiber node for Counter
fiber.memoizedState = null (no hooks yet)
2. React calls Counter()
3. Counter calls useState(0)
React is in "mount" mode for this component
React creates a new hook state object:
{
memoizedState: 0, ← the initial value
baseState: 0,
queue: {
pending: null,
dispatch: setCount, ← bound to THIS fiber's update queue
},
next: null
}
Attaches it to fiber.memoizedState
Returns [0, setCount]
4. Counter returns JSX: <div><p>Count: 0</p><button>+</button></div>
5. React commits: creates real DOM nodes
button.addEventListener('click', increment) ← increment closes over count=0
Click (update):
1. User clicks button
2. Browser fires click event
3. increment() executes
4. setCount(count + 1) = setCount(0 + 1) = setCount(1)
5. React enqueues an update on the hook's queue:
queue.pending = { action: 1, next: null }
6. React marks Counter's Fiber with pending work:
fiber.lanes = SyncLane (needs synchronous re-render)
7. React schedules a re-render via the scheduler
8. Current event handler finishes
9. React begins render phase for Counter's Fiber
10. React calls Counter() again
11. Counter calls useState(0)
React is in "update" mode for this component
React reads the hook from fiber.memoizedState (same linked list position)
React processes the update queue:
queue.pending.action = 1
New memoizedState = 1
Clears queue.pending
Returns [1, setCount]
12. Counter() executes with count = 1
Returns JSX: <div><p>Count: 1</p><button>+</button></div>
13. Reconciler diffs new element tree against previous:
<p> children changed: "Count: 0" → "Count: 1"
<button> unchanged
14. Commit phase:
p.textContent = "Count: 1" ← one DOM write
button: untouched
15. fiber.memoizedState.memoizedState = 1 ← persisted for next render
The key moment is step 11. useState(0) is called again, but React ignores the 0. It reads from the linked list. The 0 is not used. This is why the initializer can be an expensive computation without being memoized: useState(expensiveComputation()) runs expensiveComputation() on every render because the argument is evaluated before useState is called, even though React throws the result away after mount. The correct form for expensive initializers is the lazy initializer: useState(() => expensiveComputation()). React calls the function only on mount and discards it afterward.
Also notice step 5: increment closes over count = 0. It always will, because increment was created during the render where count was 0. This is the stale closure problem. The count inside increment is frozen at the value from the render that created increment. If you called setCount(count + 1) multiple times inside increment, you would always add 1 to 0, not to the accumulating value. This is not a React bug. It is JavaScript closures working exactly as designed. The functional updater form setCount(prev => prev + 1) bypasses the closure by passing a function that React calls with the latest state, not the captured snapshot.
7. Interview Traps
Trap 1: What does this log, and what does the screen show?
function Counter() {
const [count, setCount] = React.useState(0);
function handleClick() {
setCount(count + 1);
console.log(count);
}
return <button onClick={handleClick}>Count: {count}</button>;
}Click the button three times.
Answer
The console logs 0, 0, 0 on the first three clicks, not 0, 1, 2. The screen shows 1, 2, 3 correctly.
Here is why. count inside handleClick is the snapshot from the render when handleClick was created. On the first render, count is 0. handleClick closes over 0. Clicking calls setCount(1) and logs 0. React re-renders. A new handleClick is created, closing over the new count = 1. The second click logs 1 (not 0 this time), calls setCount(2). The third click logs 2, calls setCount(3).
Wait, re-reading: the first click logs 0. The second click logs 1. The third click logs 2. Always one behind what the screen shows. The screen shows the committed state after re-render. The log shows the state at the time the click handler ran, which is the previous render’s value. setCount enqueues the update. The log runs immediately after, before the re-render. So count in the log is always one render behind the visible count.
The trap is that developers expect console.log(count) after setCount(count + 1) to show the new value. It never does, because setCount does not mutate count. It schedules a re-render. The new value is only available in the next render’s execution of the function.
Trap 2: Why does this not work as expected? The user expects the button to show the count incrementing.
function Counter() {
let count = 0;
function increment() {
count++;
document.getElementById('count-display').textContent = count;
}
return (
<div>
<p id="count-display">0</p>
<button onClick={increment}>+</button>
</div>
);
}It appears to work. What is the hidden failure?
Answer
The visible behavior appears correct: clicking the button increments the number on screen. But this is working by bypassing React entirely. count++ mutates a local variable. document.getElementById reaches into the real DOM and directly mutates a text node. React has no knowledge of this. React’s Virtual DOM still represents <p id="count-display">0</p>. If anything causes a React re-render (a parent state change, a context update, anything), React will reconcile its Virtual DOM against the real DOM, find that its virtual p has text “0”, and overwrite the DOM with “0”, resetting the count. The direct DOM mutation is not durable. It exists only until React’s next commit phase touches that node. This is the exact reason React owns the DOM: any write outside React is silently overwritten on the next reconciliation. The counter appears to work in isolation but fails the moment the component lives in a real application with other state.
Trap 3: What is wrong with this useState initializer?
function ProductList() {
const [products, setProducts] = React.useState(fetchProductsFromAPI());
return (
<ul>
{products.map((p) => (
<li key={p.id}>{p.name}</li>
))}
</ul>
);
}Answer
fetchProductsFromAPI() is called on every render, not just the first. JavaScript evaluates function arguments before calling the function. So useState(fetchProductsFromAPI()) first evaluates fetchProductsFromAPI(), then passes the result to useState. This happens every render. But useState uses the initializer only on mount. Every render after the first, the return value of fetchProductsFromAPI() is computed and then discarded. If the function is synchronous and expensive (reading from localStorage, processing large data), this is wasted computation on every render. If it is asynchronous, it returns a Promise and products is initialized to a Promise object, not the resolved data, which breaks the render entirely.
The fix for expensive synchronous initializers is the lazy initializer form: useState(() => fetchProductsFromAPI()). React calls this function only on mount. On subsequent renders, the function reference is evaluated (cheap) but not called. For async data fetching, useState(null) as the initial value and a useEffect to fetch, or a data-fetching library, is the correct pattern.
Trap 4: Will this work? Why or why not?
let externalCount = 0;
function Counter() {
function increment() {
externalCount++;
// force re-render somehow
}
return <p>{externalCount}</p>;
}What fundamental problem remains even if you solve the “force re-render” part?
Answer
Even if you manually force a re-render (by, say, using a dummy state variable as a trigger), the fundamental architectural problem is that externalCount is module-level state. If Counter is rendered in two places simultaneously, both instances share the same externalCount. Incrementing in one instance increments for all instances. There is no per-instance state. useState creates state that is scoped to a specific Fiber node, meaning a specific instance of a component in a specific position in the tree. Two Counter components have two independent state slots. Module-level variables have one shared slot for all instances. This is why React’s state model stores values on the Fiber, not in closures or module scope: Fiber-scoped state is automatically per-instance without any extra work.
Self-Test
Q1. Name the three distinct mechanical reasons a plain let variable inside a component function cannot drive UI updates. Each reason is a different layer of the problem.
Answer
First, the signal problem: React does not observe JavaScript variable assignments. When count = count + 1 executes, React receives no notification. No Fiber is marked dirty. No re-render is scheduled. The screen does not update. Second, the survival problem: local variables inside a function are created fresh on every call and destroyed when the function returns. If a re-render is somehow triggered, the function runs again, the variable is initialized to its starting value, and the incremented value is gone. Third, the scope problem: even if the variable is moved outside the function to survive across calls, React still has no mechanism to associate that variable with a specific component instance or know which subtree to re-render when it changes. Module-level variables are shared across all instances, and mutation of them cannot be targeted to a single component’s Fiber.
Q2. useState’s initializer argument is evaluated on every render but only used on the first. What does this mean for performance, and how do you fix it?
Answer
JavaScript evaluates all function arguments before calling the function. useState(expensiveComputation()) calls expensiveComputation() first, passes the result to useState, and useState discards the result on every render after the first. The computation runs on every render wastefully. The fix is the lazy initializer: useState(() => expensiveComputation()). You pass a function reference, not the result. React calls the function only on mount and ignores it on subsequent renders. The function reference is evaluated (reading a variable, essentially free) on every render, but the function body runs only once. This pattern applies to any expensive synchronous initial value: reading from localStorage, parsing a large dataset, or computing an initial sort.
Q3. The setCount function returned by useState is described as “bound to a specific Fiber’s update queue.” What does this mean, and why does it matter for components rendered in multiple places?
Answer
When React creates a Fiber node for a component instance, it creates a state hook object on that Fiber with an update queue. The dispatch function (the setter) returned by useState is created with a closure that references that specific Fiber’s queue. Calling setCount enqueues an update on that specific Fiber and marks that specific Fiber as needing work. If the same component is rendered in two places in the tree, there are two Fiber nodes, two hook state objects, two update queues, and two setter functions. Calling the setter from one instance affects only that instance’s Fiber. The other instance is unaffected. This is per-instance state by design. It is the reason a Counter component can be used 50 times on a page with each counter being independent: each instance has its own Fiber, its own state slot, and its own setter bound to its own queue.
Q4. Why does console.log(count) immediately after setCount(count + 1) always show the old value, not the new one?
Answer
setCount does not mutate the count variable. count is a constant in the current render’s scope: the value read from memoizedState when React called the component function. It cannot change during the function’s execution. setCount enqueues an update on the Fiber’s queue and schedules a re-render. It returns immediately without updating anything visible to the current execution. The new state value only exists after React processes the queue and calls the component function again on the next render. The count you read after calling setCount is the same snapshot it was before. To read the new value after setting it, you must wait for the next render, which means reading it in the function body on that next call, not immediately after the setter.
Q5. A plain variable inside a component function works correctly if all you ever do is read it. When does reading a plain local variable produce incorrect results, and what is the exact condition that causes it?
Answer
A plain local variable is incorrect to read when it depends on a value that was computed in a previous render. Because local variables are recreated on every function call, any value that needs to persist across renders (survive the function returning and being called again) cannot be a local variable. The exact condition: if the value’s correct current state requires knowledge of what it was on the previous render, and the current render cannot recompute it from scratch using only the current props and other state, then it must be stored outside the function in either useState (if it drives UI) or useRef (if it doesn’t). A local variable is safe to read when it is freshly computed from current state and props on every render, which is exactly what derived values are. The error is using a local variable as if it has memory when JavaScript function-local variables have none.