fix: setState callback dropped when update is falsy#5132
Open
chatman-media wants to merge 2 commits into
Open
Conversation
If the update arg to setState is null/undefined (either passed directly, or returned from an updater function bailing out), the function returns early without ever queuing the callback or calling enqueueRender. Since callbacks only flush during a render pass, this silently drops the callback for good instead of just deferring it. Returning null/undefined from an updater is an explicitly supported pattern per the TS types, so setState(() => null, cb) should still run cb even though there's nothing to apply.
📊 Tachometer Benchmark ResultsSummary⏳ Benchmarks are currently running. Results below are out of date. duration
usedJSHeapSize
Results⏳ Benchmarks are currently running. Results below are out of date. create10kduration
usedJSHeapSize
filter-listduration
usedJSHeapSize
hydrate1kduration
usedJSHeapSize
many-updatesduration
usedJSHeapSize
replace1kduration
usedJSHeapSize
run-warmup-0
run-warmup-1
run-warmup-2
run-warmup-3
run-warmup-4
run-final
text-updateduration
usedJSHeapSize
tododuration
usedJSHeapSize
update10th1kduration
usedJSHeapSize
|
Member
|
👋 thanks for the PR! We can't however merge this as-is as it would create a new gap for preact/compat. In react we don't enqueue a render but the callback is called so while this solves half the problem it introduces a redundant render cycle. |
setState(falsy, callback) was enqueueing a full render just to flush the callback, even when there was nothing to commit. Now it only enqueues a render when one isn't already pending for the component (so a callback tacked onto a real update still batches with it); otherwise it invokes the callback right away, matching React's behavior for a no-op update. Errors thrown from that callback go through the same error-boundary path commitRoot uses, so behavior stays consistent either way. Added coverage for: no render triggered for a lone no-op callback, multiple stacked no-op callbacks each firing once, and a no-op callback correctly batching onto an already-pending render instead of jumping the queue.
Author
|
Good catch — pushed a fix. Now it only enqueues a render when one isn't already pending for the component; otherwise the callback just fires right away since there's nothing to commit. Added tests that assert no extra render happens for the lone no-op case, and that it still batches properly when there's a real update alongside it. |
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 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.
setState(() => null, cb) never calls cb. The early-return for a falsy update happens before the callback ever gets pushed onto
_stateCallbacks, and since that array only flushes during a render pass triggered byenqueueRender, the callback just never runs — not deferred, just gone.This matters because bailing out of an update by returning null/undefined from the updater function is explicitly typed as valid (
index.d.tsallowsPick<S,K> | Partial<S> | null), so it's a real pattern people rely on, not an edge case someone made up.setState(null, cb)has the same problem.Fixed by only skipping the early return when there's a callback to flush — if there's truly nothing to apply and nothing to call, it still bails out the same as before, so the perf shortcut is intact. Added tests for both the updater-returns-null case and passing null directly.