Skip to content

fix: setState callback dropped when update is falsy#5132

Open
chatman-media wants to merge 2 commits into
preactjs:mainfrom
chatman-media:fix/setstate-callback-falsy-update
Open

fix: setState callback dropped when update is falsy#5132
chatman-media wants to merge 2 commits into
preactjs:mainfrom
chatman-media:fix/setstate-callback-falsy-update

Conversation

@chatman-media

Copy link
Copy Markdown

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 by enqueueRender, 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.ts allows Pick<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.

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.
@github-actions

github-actions Bot commented Jun 30, 2026

Copy link
Copy Markdown

📊 Tachometer Benchmark Results

Summary

⏳ Benchmarks are currently running. Results below are out of date.

duration

  • create10k: unsure 🔍 -1% - +0% (-7.47ms - +1.34ms)
    preact-local vs preact-main
  • filter-list: unsure 🔍 -1% - +0% (-0.19ms - +0.05ms)
    preact-local vs preact-main
  • hydrate1k: unsure 🔍 -2% - +2% (-1.08ms - +0.97ms)
    preact-local vs preact-main
  • many-updates: unsure 🔍 -1% - +1% (-0.22ms - +0.17ms)
    preact-local vs preact-main
  • replace1k: unsure 🔍 -2% - +1% (-1.02ms - +0.73ms)
    preact-local vs preact-main
  • text-update: unsure 🔍 -3% - +5% (-0.06ms - +0.10ms)
    preact-local vs preact-main
  • todo: unsure 🔍 -2% - +1% (-0.54ms - +0.36ms)
    preact-local vs preact-main
  • update10th1k: unsure 🔍 -3% - +4% (-0.90ms - +1.26ms)
    preact-local vs preact-main

usedJSHeapSize

  • create10k: unsure 🔍 -0% - +0% (-0.00ms - +0.00ms)
    preact-local vs preact-main
  • filter-list: unsure 🔍 -0% - +0% (-0.00ms - +0.01ms)
    preact-local vs preact-main
  • hydrate1k: unsure 🔍 -4% - +4% (-0.20ms - +0.20ms)
    preact-local vs preact-main
  • many-updates: unsure 🔍 -0% - +0% (-0.00ms - +0.00ms)
    preact-local vs preact-main
  • replace1k: unsure 🔍 -0% - -0% (-0.01ms - -0.00ms)
    preact-local vs preact-main
  • text-update: unsure 🔍 -3% - +3% (-0.03ms - +0.03ms)
    preact-local vs preact-main
  • todo: unsure 🔍 +0% - +0% (+0.00ms - +0.00ms)
    preact-local vs preact-main
  • update10th1k: unsure 🔍 -0% - +0% (-0.01ms - +0.01ms)
    preact-local vs preact-main

Results

⏳ Benchmarks are currently running. Results below are out of date.
create10k

duration

VersionAvg timevs preact-localvs preact-main
preact-local821.69ms - 827.75ms-unsure 🔍
-1% - +0%
-7.47ms - +1.34ms
preact-main824.59ms - 830.98msunsure 🔍
-0% - +1%
-1.34ms - +7.47ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local19.07ms - 19.07ms-unsure 🔍
-0% - +0%
-0.00ms - +0.00ms
preact-main19.07ms - 19.07msunsure 🔍
-0% - +0%
-0.00ms - +0.00ms
-
filter-list

duration

VersionAvg timevs preact-localvs preact-main
preact-local16.56ms - 16.60ms-unsure 🔍
-1% - +0%
-0.19ms - +0.05ms
preact-main16.54ms - 16.77msunsure 🔍
-0% - +1%
-0.05ms - +0.19ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local1.55ms - 1.55ms-unsure 🔍
-0% - +0%
-0.00ms - +0.01ms
preact-main1.55ms - 1.55msunsure 🔍
-0% - +0%
-0.01ms - +0.00ms
-
hydrate1k

duration

VersionAvg timevs preact-localvs preact-main
preact-local57.70ms - 59.16ms-unsure 🔍
-2% - +2%
-1.08ms - +0.97ms
preact-main57.77ms - 59.20msunsure 🔍
-2% - +2%
-0.97ms - +1.08ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local5.00ms - 5.29ms-unsure 🔍
-4% - +4%
-0.20ms - +0.20ms
preact-main5.00ms - 5.29msunsure 🔍
-4% - +4%
-0.20ms - +0.20ms
-
many-updates

duration

VersionAvg timevs preact-localvs preact-main
preact-local16.29ms - 16.65ms-unsure 🔍
-1% - +1%
-0.22ms - +0.17ms
preact-main16.42ms - 16.58msunsure 🔍
-1% - +1%
-0.17ms - +0.22ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local3.74ms - 3.74ms-unsure 🔍
-0% - +0%
-0.00ms - +0.00ms
preact-main3.74ms - 3.74msunsure 🔍
-0% - +0%
-0.00ms - +0.00ms
-
replace1k
  • Browser: chrome-headless
  • Sample size: 100
  • Built by: CI #5607
  • Commit: 46ff9f1

duration

VersionAvg timevs preact-localvs preact-main
preact-local55.04ms - 56.24ms-unsure 🔍
-2% - +1%
-1.02ms - +0.73ms
preact-main55.14ms - 56.42msunsure 🔍
-1% - +2%
-0.73ms - +1.02ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local3.01ms - 3.01ms-unsure 🔍
-0% - -0%
-0.01ms - -0.00ms
preact-main3.01ms - 3.02msunsure 🔍
+0% - +0%
+0.00ms - +0.01ms
-

run-warmup-0

VersionAvg timevs preact-localvs preact-main
preact-local26.27ms - 26.95ms-unsure 🔍
-2% - +1%
-0.64ms - +0.27ms
preact-main26.49ms - 27.10msunsure 🔍
-1% - +2%
-0.27ms - +0.64ms
-

run-warmup-1

VersionAvg timevs preact-localvs preact-main
preact-local30.78ms - 31.78ms-unsure 🔍
-2% - +2%
-0.77ms - +0.62ms
preact-main30.88ms - 31.83msunsure 🔍
-2% - +2%
-0.62ms - +0.77ms
-

run-warmup-2

VersionAvg timevs preact-localvs preact-main
preact-local28.77ms - 29.77ms-unsure 🔍
-2% - +3%
-0.53ms - +0.93ms
preact-main28.54ms - 29.60msunsure 🔍
-3% - +2%
-0.93ms - +0.53ms
-

run-warmup-3

VersionAvg timevs preact-localvs preact-main
preact-local24.98ms - 25.32ms-unsure 🔍
-0% - +1%
-0.12ms - +0.28ms
preact-main24.97ms - 25.17msunsure 🔍
-1% - +0%
-0.28ms - +0.12ms
-

run-warmup-4

VersionAvg timevs preact-localvs preact-main
preact-local23.19ms - 24.57ms-unsure 🔍
-5% - +3%
-1.25ms - +0.63ms
preact-main23.55ms - 24.83msunsure 🔍
-3% - +5%
-0.63ms - +1.25ms
-

run-final

VersionAvg timevs preact-localvs preact-main
preact-local20.57ms - 21.01ms-unsure 🔍
-1% - +2%
-0.30ms - +0.32ms
preact-main20.56ms - 20.99msunsure 🔍
-2% - +1%
-0.32ms - +0.30ms
-
text-update
  • Browser: chrome-headless
  • Sample size: 220
  • Built by: CI #5607
  • Commit: 46ff9f1

duration

VersionAvg timevs preact-localvs preact-main
preact-local1.79ms - 1.92ms-unsure 🔍
-3% - +5%
-0.06ms - +0.10ms
preact-main1.78ms - 1.89msunsure 🔍
-5% - +3%
-0.10ms - +0.06ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local1.02ms - 1.06ms-unsure 🔍
-3% - +3%
-0.03ms - +0.03ms
preact-main1.01ms - 1.05msunsure 🔍
-3% - +3%
-0.03ms - +0.03ms
-
todo

duration

VersionAvg timevs preact-localvs preact-main
preact-local29.55ms - 29.90ms-unsure 🔍
-2% - +1%
-0.54ms - +0.36ms
preact-main29.40ms - 30.22msunsure 🔍
-1% - +2%
-0.36ms - +0.54ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local1.26ms - 1.26ms-unsure 🔍
+0% - +0%
+0.00ms - +0.00ms
preact-main1.26ms - 1.26msunsure 🔍
-0% - -0%
-0.00ms - -0.00ms
-
update10th1k

duration

VersionAvg timevs preact-localvs preact-main
preact-local29.22ms - 30.75ms-unsure 🔍
-3% - +4%
-0.90ms - +1.26ms
preact-main29.05ms - 30.56msunsure 🔍
-4% - +3%
-1.26ms - +0.90ms
-

usedJSHeapSize

VersionAvg timevs preact-localvs preact-main
preact-local2.96ms - 2.97ms-unsure 🔍
-0% - +0%
-0.01ms - +0.01ms
preact-main2.96ms - 2.97msunsure 🔍
-0% - +0%
-0.01ms - +0.01ms
-

tachometer-reporter-action v2 for CI

@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 98.491%. remained the same — chatman-media:fix/setstate-callback-falsy-update into preactjs:main

@JoviDeCroock

Copy link
Copy Markdown
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.
@chatman-media

Copy link
Copy Markdown
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants