Uh oh!
There was an error while loading. Please reload this page.
[flag] warn for setState in render on initial mount - #35084
Open
rickhanlonii wants to merge 5 commits into
Open
Conversation
Comparing: 4842fbe...7820ad0 Critical size changesIncludes critical production bundles, as well as any change greater than 2%:
Significant size changesIncludes any change greater than 0.2%: Expand to show
|
gaearon
commented
Nov 9, 2025
Collaborator
Maybe “during the initial render”? Set state “on mount” usually refers to doing it in an effect which isn’t what this seems to be about. Also, “this behavior is deprecated” maybe could be “this is deprecated, pass the initial value to useState instead” or something like that so it’s clear how to fix it. |
rickhanloniiforce-pushed
the
rh/set-state-mount-error
branch
2 times, most recently
from
November 11, 2025 20:56
11f09d6 to
96509c5Comparerickhanlonii
requested review from
acdlite and sebmarkbage
and removed request for
sebmarkbageNovember 11, 2025 21:29
rickhanlonii
commented
Nov 11, 2025
ContributorAuthor
Thanks @gaearon, message updated. |
rickhanloniiforce-pushed
the
rh/set-state-mount-error
branch
from
November 11, 2025 21:42
2f8d8ba to
dca4fccComparerickhanloniiforce-pushed
the
rh/set-state-mount-error
branch
from
January 21, 2026 16:01
dca4fcc to
d18a9ecComparerickhanloniiforce-pushed
the
rh/set-state-mount-error
branch
from
February 4, 2026 15:17
d18a9ec to
48f80d6Compareacdlite
previously approved these changes
Feb 4, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for freeto join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
Add a feature flag
disableSetStateInRenderOnMountto start experimenting with warning for setState in initial render.Motivation
The main reason to start warning about this now is that supporting this prevents us from landing SSR optimizations such as inlining hooks and removing code to support updates during SSR. But this also indicates a performance issue in the app that can usually be easily fixed.
Pattern isn't useful
Usually, calling
setStatein initial render of a component is an accident, by forgetting to pass the initial value intouseState():For those use cases, the warning will fire and applying an easy fix to pass the inital state will give performance benefits since we won't need to immediately re-render a component on mount.
Now this will setState in render on updates.
Future SSR improvements
If this pattern is fixed, we could compile the SSR bundle code from:
To:
This could be a huge speedup in SSR performance.
Experiment
To confirm how many cases fall into the easy fix, I'm rolling out a feature flag to test it in a large app to see what use cases are depending on this, and how difficult the fixes are.
Review
It's a pain to update the tests for Fiber and Fizz in different PRs because of the server integration tests.
Here's how the different warnings are implemented in separate commits: