React useState: Does Calling setState with the Same Value Re-render?
With React's useState, passing the current value to the set function usually does not trigger a re-render. However, the component function was called, and the onRender of <Profiler> was also called, once: the single time right after the value changed when the same value was passed again. If you call setState on every scroll or keystroke and want to check for wasted re-renders, this shows roughly what React skips.
In my test environment, I wrapped the component in <Profiler> and recorded how many times the component function was called and how many times onRender was called after each button press.
The test environment was Ubuntu 24.04, Node.js 22.23.4, React 19.3.1, TypeScript 7.0.2, esbuild 0.28.2, Playwright 1.63.0, and Chromium 153. I type-checked the .tsx files with tsc, bundled them with esbuild, loaded them into Chromium with Playwright, and pressed the buttons. I tested both a development build and a production build with profiling enabled, and the results were the same. I did not test the React Developer Tools profiler (browser extension), useReducer, or class components.

How the counts are recorded
I count how many times the component function is called at the top of the function. I also wrap the component in <Profiler> and record how many times onRender is called and the phase argument. phase is "mount" on the first render, and "update" when a re-render is caused by a state change or similar (the argument description for onRender in the official reference).
// log.ts
import type { ProfilerOnRenderCallback } from 'react';
export const calls: Record<string, number> = {};
export const commits: string[] = [];
export function countCall(name: string) {
calls[name] = (calls[name] ?? 0) + 1;
}
export const onRender: ProfilerOnRenderCallback = (id, phase) => {
commits.push(`${id}:${phase}`);
};
The component under test is as follows. The buttons in the demo are labeled in Japanese, but in this article I refer to them by their English meaning. The "same value" button passes the current count straight to setCount. The child component Child also counts how many times it is called.
function Child() {
countCall('Child');
return <span>子</span>;
}
export function SameNumber() {
countCall('SameNumber');
const [count, setCount] = useState(0);
return (
<div>
<button onClick={() => setCount(count + 1)}>+1</button>
<button onClick={() => setCount(count)}>同じ値</button>
<output>{count}</output>
<Child />
</div>
);
}
I rendered this wrapped in <Profiler id="SameNumber" onRender={onRender}>. Strict Mode runs an extra render in development, which would throw off the counts, so I did not use Strict Mode.
Counts when passing the same value
I pressed "same value" twice, then "+1" once, then "same value" three times. The numbers in the table are the cumulative counts after each press.
| Button pressed | Display | Parent function | Child function | onRender |
|---|---|---|---|---|
| (initial render) | 0 | 1 | 1 | 1 (mount) |
| Same value | 0 | 1 | 1 | 1 |
| Same value | 0 | 1 | 1 | 1 |
| +1 | 1 | 2 | 2 | 2 (update) |
| Same value | 1 | 3 | 2 | 3 (update) |
| Same value | 1 | 3 | 2 | 3 |
| Same value | 1 | 3 | 2 | 3 |
When I pressed "same value" after the initial render, none of the counts increased. The component function was not called, and onRender was not called either.
On the other hand, when I pressed "same value" right after "+1" changed the value, the parent SameNumber function was called once, and onRender was called once with "update". The child Child function was not called. After that, pressing "same value" any number of times did not increase any count.
The official reference for useState says, in its caveats for the set function, that if the new value is identical to the current state, React skips re-rendering the component and its children. The same section also says that in some cases React may still need to call the component before skipping its children, but that this should not affect your code. The single render right after the value changed matched this: only the parent was called, the child was not, and the display did not change.
When passing the same value through an updater function
I pressed the buttons in the same order when passing an updater function that returns the value it receives, such as setCount((c) => c).
<button onClick={() => setCount((c) => c + 1)}>+1</button>
<button onClick={() => setCount((c) => c)}>同じ値</button>
The results were the same as passing the value directly. The two "same value" presses at the start did not increase any count. Only the one "same value" press right after "+1" increased the parent function count and onRender by one each, and the child count did not change.
Objects are compared by identity, not by contents
When React compares a new value with the current state, it uses Object.is (caveats for the set function). For objects, this means that a different object is treated as a different value even if its contents are the same.
Passing a new object with the same contents re-renders every time
type User = { name: string };
export function NewObject() {
countCall('NewObject');
const [user, setUser] = useState<User>({ name: 'たま' });
return (
<div>
<button onClick={() => setUser({ ...user })}>同じ中身</button>
<output>{user.name}</output>
<Child />
</div>
);
}
{ ...user } is a new object with the same contents. When I pressed "same contents" three times, the parent function, the child function, and onRender each increased by one per press (all reached 4 after three presses). The display stayed at "Tama" and did not change.
Mutating an object and passing it back does not update the screen
export function MutateObject() {
countCall('MutateObject');
const [user, setUser] = useState<User>({ name: 'たま' });
return (
<div>
<button
onClick={() => {
user.name = 'みけ';
setUser(user);
}}
>
書き換え
</button>
<output>{user.name}</output>
<Child />
</div>
);
}
This changes user.name to "Mike" ('みけ' in the code) and then passes the same user object to setUser. Pressing the "mutate" button twice did not increase the parent function count, the child function count, or onRender, and the display stayed at "Tama". Because the object passed is the same one as before, React judges that nothing changed and skips the re-render.
The official reference explains this behavior in "I updated the state, but the screen doesn't update". To change an object state, create a new object and pass it in, such as setUser({ ...user, name: 'みけ' }) (the official explanation of treating state as read-only).
The NaN and -0 cases
Object.is treats NaN and -0 differently from ===. It considers two NaN values the same, and 0 and -0 different (MDN's explanation of Object.is).
const [n, setN] = useState(NaN);
const [z, setZ] = useState(0);
// ...
<button onClick={() => setN(NaN)}>NaN</button>
<button onClick={() => setZ(-0)}>-0</button>
In my test environment, passing NaN to a state that already held NaN called neither the function nor onRender. NaN === NaN is false, but React treats them as the same value.
Passing -0 to a state holding 0 triggered a re-render. When I passed -0 again, the function and onRender were each called once, since that was the single render right after the value changed. The third -0 call did not call anything.





