Don't flush interactive updates to uncontrolled components early #13507
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.
This is related to what @NE-SmallTown noticed in #13488 (comment).
In the first commit, I amend my test to check for more things. Specifically, it seems weird that
setState
inside anonChange
handler in async mode was flushed synchronously upon exiting an event — even though there was just one event. So I assert that it shouldn’t do that.The cause turns out related to this:
react/packages/events/ReactGenericBatching.js
Lines 44 to 51 in 1c0ba70
We need to flush restore for controlled inputs at the end of the interactive event. But we currently do this regardless of whether an input is controlled or not:
react/packages/react-dom/src/events/ChangeEventPlugin.js
Lines 59 to 60 in 1c0ba70
In the second commit, I change it so that we don't flush interactive updates early for cases where the inputs aren't controlled (or affect other controlled inputs).
I'm not very familiar with this code and not entirely sure this is right. For example, an input might flip from uncontrolled to controlled, and we kinda support that (with a warning)? Then I think this detection would fail.
Also not sure it's worth the added complexity in practice. If you think it's valuable I can add more test coverage.