Skip to content
Source
Sign inSign up
TanStack/redact

fix: class setState callbacks, batchedUpdates argument, and default useSyncExternalStoreWithSelector #72

ClosedMohakBajaj(GitHub profile, opens in a new tab)wants to merge 3 commits intomain fromrefs/pull/72/headopened

Summary

These are three small React parity gaps that broke a TanStack Start app (Zustand 4 + Excalidraw) on 0.2.0. Each fix is its own commit with its own test.

1. useSyncExternalStoreWithSelector is missing from the default export. The Vite plugin aliases use-sync-external-store/shim/with-selector to the package root. Zustand 4 default-imports that module, so every store threw useSyncExternalStoreWithSelector is not a function. During SSR, every route returned a 500.

 // react/index.ts: default export
   useSyncExternalStore,
+  useSyncExternalStoreWithSelector,

2. unstable_batchedUpdates(fn, a) drops a. React calls fn(a). Excalidraw wraps its pointer and keyboard handlers as batchedUpdates(handler, event), so every handler got undefined and threw Cannot read properties of undefined (reading 'clientX').

-export function batchedUpdates<T>(fn: () => T): T {
+export function batchedUpdates<A, T>(fn: (a: A) => T, a?: A): T {
   ...
-    return fn();
+    return fn(a as A);

3. Class setState / forceUpdate callbacks only run on unmount. _u pushed the callback onto fiber.e, which is the fiber's cleanup list. Excalidraw starts loading its scene from setState(…, cb) in componentDidMount, so it stayed on "Loading scene…" forever.

The callback is now staged as a lifecycle commit of the fiber's next render, using the existing staged-commit queue. Staging it with the ref flag orders it after componentDidUpdate, which matches React's componentDidUpdate → callbacks → refs.

 instance._u = (cb) => {
   if (cb)
-    (fiber.e ||= []).push(cb);
+    scheduleLifecycle(fiber, () => cb.call(instance), true);
   scheduleUpdate(fiber);
 };

-export function scheduleLifecycle(fiber, fn) {
-  if (isActivityHidden(fiber) || layoutIsDisconnected(fiber))
+export function scheduleLifecycle(fiber, fn, ref?) {
+  if (fiber.u || isActivityHidden(fiber) || layoutIsDisconnected(fiber))
     return;
-  stageCommit(fiber, fn);
+  stageCommit(fiber, fn, ref);

The fiber.u guard keeps a callback from an already-unmounted instance from staying in stagedCommits forever.

Evidence

New tests. All 7 fail on main and pass on this branch:

batched-updates.test.tsx
  × → ✓ passes its second argument to the callback and returns the result, like React
  × → ✓ batches state updates made with the forwarded argument
class-set-state-callback.test.tsx
  × → ✓ runs a setState callback after the update commits, not on unmount       // [didUpdate, callback 1]
  × → ✓ runs a setState callback queued from componentDidMount                  // the Excalidraw pattern
  × → ✓ runs batched setState and forceUpdate callbacks once each, in call order
  × → ✓ runs a setState callback even when shouldComponentUpdate skips the render
external-store-selector.test.tsx
  × → ✓ is on the default export, which use-sync-external-store/shim/with-selector aliases to

Full gate (Node 24.22.0 from .nvmrc):

  • pnpm test: 157 files, 1866 passed.
  • pnpm test:types: pass.
  • pnpm build: build verification passes.
  • pnpm size:check: all budgets pass. dist redact/dom goes from 25544 → 25551 B gzip (budget 25556), and dist client total (default Vite) from 23304 → 23310 B.

In the app. Zustand stores render during SSR. In Excalidraw, the scene loads, drawing a rectangle works and undo works, with no console errors. Results match the same app built on React.

Merge Danger

Door: two-way

Each commit reverts cleanly on its own.

Blast Radius: narrow

  • Class components that pass a callback to setState or forceUpdate now get it after commit instead of at unmount.
  • unstable_batchedUpdates callers that pass a second argument now receive it.
  • The classComponents: false stub has the same fiber.e push. I left it alone because the stub explicitly drops lifecycles, but it has the same wrong behaviour.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Class-component update callbacks now run after updates are committed, with consistent ordering and without repeating on unmount.
    • Batched updates now forward callback arguments and return callback results.
  • New Features
    • The default React export now includes useSyncExternalStoreWithSelector.
    1. 🦋 Changeset detected

      Latest commit: 12cb1419bb064785fea84edc45a01a1e0a5874bb

      The changes in this PR will be included in the next version bump.

      This PR includes changesets to release 1 package
      NameType
      @tanstack/redactPatch

      Not sure what this means? Click here to learn what changesets are.

      Click here if you're a maintainer who wants to add another changeset to this PR

    2. chatgpt-codex-connector[bot](GitHub profile, opens in a new tab)commented

      Codex Review Summary

      This comment shows the latest Codex review activity on this pull request.

      ReviewStatusCommitReview trigger
      📝 Code Review⚠️ Failed 2026-10-26T20:10:14.673214Z
      We couldn’t complete this review. Comment @codex review to try again.
      12cb141PR opened
      ℹ️ About Codex in GitHub

      Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

      • Open a pull request for review
      • Mark a draft as ready
      • Comment "@codex review" or "@codex security review".

      Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

    3. coderabbitai[bot](GitHub profile, opens in a new tab)commented
      Review in Change Stack →

      No actionable comments were generated in the recent review.

      Recent review info
      Run configuration
      • Source mode: Auto
      • Repository: Unavailable
      • Branch: Unavailable
      • File: No configuration file from this repository applied
      • Commit: Unavailable
      • Layers: defaults
      • Review profile: CHILL
      • Plan: Advanced
      • Run ID: c326bbae-e376-4642-a385-700bc8157d80
      Commits

      Reviewing files that changed from the base of the PR and between d56d87cbe3bbdc412da836026a81f6a2b2da2ce5 and 12cb1419bb064785fea84edc45a01a1e0a5874bb.

      Files selected for processing: 7
      • .changeset/react-api-parity-gaps.md
      • packages/redact/src/dom/features/class/full.ts
      • packages/redact/src/dom/reconcile.ts
      • packages/redact/src/react/index.ts
      • tests/batched-updates.test.tsx
      • tests/class-set-state-callback.test.tsx
      • tests/external-store-selector.test.tsx

      Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.

      Walkthrough

      The changes update class-component callback scheduling, forward optional arguments through batched update APIs, and add useSyncExternalStoreWithSelector to the React default export. New tests cover each change, and a patch changeset records the fixes.

      Changes: React API parity

      Layer / File(s)Summary
      Class update callback lifecycle
      packages/redact/src/dom/reconcile.ts, packages/redact/src/dom/features/class/full.ts, tests/class-set-state-callback.test.tsx
      Class update callbacks use lifecycle scheduling with the component instance as this. Tests cover callback timing, ordering, and updates when rendering is skipped.
      Batched update argument forwarding
      packages/redact/src/dom/reconcile.ts, tests/batched-updates.test.tsx
      batchedUpdates accepts and forwards an optional callback argument. Tests check argument forwarding, return values, and batched state updates.
      Selector hook default export
      packages/redact/src/react/index.ts, tests/external-store-selector.test.tsx, .changeset/react-api-parity-gaps.md
      The React default export includes useSyncExternalStoreWithSelector. The test checks that it matches the named import, and the changeset records the API fixes.
      Priority: Normal

      Estimated code review effort: 3 (Moderate) | ~20 minutes

      Change: Bug fix

      Suggested reviewers: tannerlinsley

      Merge Risk: Minimal · up to 12cb1

      The supplied evidence identifies no issue that needs resolution before merging; proceed with normal checks.

      Pre-merge checks: Passed 4 · Failed 1
      Failed Failed checks: 1 warning
      Check nameStatusExplanationResolution
      Docstring CoverageWarningDocstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 6 functions across 6 files. (1 skipped: 1 …Write docstrings for the functions missing them to satisfy the coverage threshold.
      Passed Passed checks: 4 passed
      Check nameStatusExplanation
      Description CheckPassedCheck skipped - CodeRabbit’s high-level summary is enabled.
      Title checkPassedThe title clearly and concisely names the three main changes: class setState callbacks, batchedUpdates argument forwarding, and the default useSyncExternalStoreWithSelector export.
      Linked Issues checkPassedCheck skipped because no linked issues were found for this pull request.
      Out of Scope Changes checkPassedCheck skipped because no linked issues were found for this pull request.
      Full details: Docstring Coverage

      Explanation

      Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 6 functions across 6 files. (1 skipped: 1 unsupported.)

      Resolution

      Write docstrings for the functions missing them to satisfy the coverage threshold.

      Finishing Touches
      🧪 Generate unit tests (beta)
      • Create a new PR
      • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

      Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

      ❤️ Share
      Comment `@coderabbitai help` to get the list of available commands.
    4. Closing as superseded: these fixes shipped in #76 and were released in 0.2.1. Thanks for picking them up.

Sign in to join this conversation.