fix(react): defer useSignal update to a microtask to avoid scheduling during commit#2099
Draft
RobHannay wants to merge 2 commits into
Draft
fix(react): defer useSignal update to a microtask to avoid scheduling during commit#2099RobHannay wants to merge 2 commits into
RobHannay wants to merge 2 commits into
Conversation
useSignal called forceUpdate() synchronously from its signal effect. When the observed signal changes while React is mid-commit - e.g. dnd-kit resets the drag operation from a layout effect at drag end, observed via useDragOperation - this scheduled a synchronous React update from within a React lifecycle method, throwing "useInsertionEffect must not schedule updates" and, when the re-render was structural, crashing reconciliation with NotFoundError: Node.removeChild. Defer the update to a microtask (both branches), mirroring the guard already in useDeepSignal. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
🦋 Changeset detectedLatest commit: 7d61b68 The changes in this PR will be included in the next version bump. This PR includes changesets to release 10 packages
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 |
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.
Problem
useSignal(used byuseComputed→useDragOperation) callsforceUpdate()synchronously from its signaleffect. When the observed signal changes while React is mid-commit, that schedules a synchronous React update from within a React lifecycle method.This happens in an ordinary scenario: a component reads drag state via
useDragOperation()and re-renders during a drag (e.g. to show/highlight a drop zone — the use case the docs describe). At drag end, dnd-kit resets the drag operation (DragOperation.reset()clearssourceIdentifier) insidemanager.renderer.rendering.then(...), which runs from the ReactRenderer's layout effect — i.e. during React's commit/effect phase. TheuseSignaleffect then firesforceUpdate()synchronously, producing:Warning: useInsertionEffect must not schedule updates.Minimal repro shape: any component under
DragDropProviderthat callsuseDragOperation()and re-renders based onsource/targetlogs the warning when a drag ends.(An earlier version of this description also attributed a
NotFoundError: removeChildcrash to this path; that turned out to be an application-side issue in how we consumedonDragOver— leaving drag-overs unanswered soOptimisticSortingPluginreordered DOM the app then also reconciled — not auseSignalbug. This PR is scoped to the warning.)Fix
Defer the
useSignalupdate to a microtask so it runs after the commit completes instead of scheduling synchronously from within the effect. This mirrors the guard already present inuseDeepSignal(queueMicrotask(() => flushSync(forceUpdate))).useDeepSignalonly deferred itssyncbranch;useSignal's async branch — the oneuseComputed/useDragOperationuse (sync=false) — has the same latent issue, so this defers both branches.Notes
packages/reactcallsuseSignal/useComputedwithsync=true, so the sync-branch change has effectively no blast radius.@dnd-kit/reactpatch).