useState and component state
The useState hook, why updates are batched, the functional-update form for updates based on previous state, and why state is isolated per component instance.
useState and component state
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- `useState(initial)` returns a pair, `[value, setValue]` — the current state and a function that updates it and schedules a re-render, destructured with array syntax because order, not name, decides which is which.
- Calling the setter does not mutate the variable in place; it tells React to re-render the component with a new value on the next pass, so code immediately after a `setX` call still sees the old value.
- React batches multiple state updates that happen inside the same event handler into a single re-render, instead of re-rendering once per `setX` call, which is why several updates in one click handler only cause one visible re-paint.
- When a new state value depends on the old one, pass a function to the setter — `setCount(prev => prev + 1)` — so React hands you the actual latest value instead of a possibly stale one captured in the closure.
- Each rendered instance of a component gets its own independent state. Two `<Counter />` elements on the same page never share a count, even though they run the exact same function.
- A component can call `useState` more than once; each call manages one independent piece of state, and there is no limit beyond the rules covered in the rules-of-hooks lesson.
A value React remembers across renders
A plain variable inside a function component resets to its initial value on every render — the function just runs again from the top. useState is how a component keeps a value alive between renders and tells React to re-render whenever that value changes:
import { useState } from "react";
function Counter() {
const [count, setCount] = useState(0);
return (
<div>
<p>Count: {count}</p>
<button onClick={() => setCount(count + 1)}>Add one</button>
</div>
);
}
useState(0) returns a two-element array: the current value (count) and a setter function (setCount). Array destructuring is what lets you name them anything — const [count, setCount] works because position, not a property name, determines which is which, unlike destructuring an object. Calling setCount does two things: it records the new value for the next render, and it tells React this component needs to re-render. It does not change count itself mid-render — count in the currently running function call stays exactly what it was when this render started.
Updates are scheduled, not immediate
This trips people up constantly:
function handleClick() {
setCount(count + 1);
console.log(count); // still the OLD value
}
setCount schedules a future render where count will be the new value — it doesn’t rewrite the count variable that this call of handleClick already closed over. If you need the new value right away, use it directly rather than reading the state variable back: const next = count + 1; setCount(next); console.log(next);.
Why two calls to setCount(count + 1) don’t add two
function handleDoubleClick() {
setCount(count + 1);
setCount(count + 1); // same count both times — only +1 total
}
Both lines read the same count captured for this render, so both schedule “become count + 1,” and the second simply overwrites the first with an identical instruction. The fix is the functional update form, which hands the setter a function instead of a value:
function handleDoubleClick() {
setCount((prev) => prev + 1);
setCount((prev) => prev + 1); // now +2, correctly
}
React calls each updater function in order, passing it the true latest pending value rather than whatever was captured in the closure when the handler started. Any time the next state depends on the previous state, reach for this form — it’s the safer default, not just a fix for double-calls.
Batching: one render, not one per call
Even with the functional form, calling setCount twice inside the same event handler does not trigger two separate re-renders. React batches every state update that happens synchronously within one event handler and applies them together, producing a single re-render with the final values. This matters for mental model more than for day-to-day code: you don’t need to worry that updating three pieces of state in one click handler will cause three flickers of re-render — React collapses them into one.
State lives on the instance, not the function
function App() {
return (
<>
<Counter />
<Counter />
</>
);
}
Both <Counter /> elements run the exact same Counter function, but React tracks state per rendered instance, not per function definition. Clicking one counter’s button never touches the other’s count. This is what “component state is local” means in practice — it’s attached to that component’s position in the tree, not to the JavaScript function itself.
What the video covers
Codevolution’s episode fixes a broken counter component by introducing useState in React 19, and makes explicitly the point that state is local per component instance and that a component can hold several independent useState calls at once — the strongest match for this lesson’s core. Batching and the functional-update form each get their own short, separate episode in the same series rather than deep treatment here, which is why they’re carried above in prose and linked as deeper videos.
Where this fits
Once two components need to react to the same piece of state, the next question is where that state should actually live — covered in lifting state up and composition.
Check yourself
After calling `setCount(count + 1)`, why does `console.log(count)` on the very next line still print the old value?
The setter does not mutate `count` in place — it schedules a re-render where `count` will have the new value. The current render's `count` variable is a plain, already-assigned value, so it stays whatever it was when this render started until the component re-renders.
Why would `setCount(count + 1); setCount(count + 1);` inside one click handler only increase the count by one, not two?
Both calls read the same `count` from this render's closure, so both compute the same next value and the second call overwrites the first with an identical result. Using the functional form, `setCount(prev => prev + 1)` twice, would correctly add two, since each call receives the true latest value.
If a page renders two `<Timer />` components, each calling `useState(0)` internally, do they share one counter or have two?
Two, completely independent. React keeps a separate state slot per rendered component instance, not per component function, so each `<Timer />` on the page tracks and updates its own count without affecting the other.
What does it mean that React batches state updates inside an event handler?
Rather than re-rendering the component after each individual `setX` call, React collects all the state updates triggered synchronously within one event handler and applies them together, producing a single re-render instead of several. This avoids wasted intermediate renders when a handler updates more than one piece of state.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.