-
Notifications
You must be signed in to change notification settings - Fork 49.8k
[flag] warn for setState in render on initial mount #35084
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rickhanlonii
wants to merge
4
commits into
facebook:main
Choose a base branch
from
rickhanlonii:rh/set-state-mount-error
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+380
−13
Conversation
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
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. |
11f09d6 to
96509c5
Compare
Member
Author
|
Thanks @gaearon, message updated. |
2f8d8ba to
dca4fcc
Compare
Sign up for free
to 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: