Skip to content

fix(lab): write latest-value refs at commit, not during render - #10780

Merged
cixzhang merged 2 commits into
meta:mainfrom
vjeux:fix/lab-ref-writes-at-commit
Nov 9, 2026
Merged

cixzhang merged 2 commits into
meta:mainfrom
vjeux:fix/lab-ref-writes-at-commit

Conversation

@vjeux

@vjeux vjeux commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

User impact

People using Lab's Comments, RefreshControl or TransferList in an app that renders with transitions. Seven files copied props or state into refs during render. A render React discards, as a suspended transition does, left its values in those refs, and the next event used them:

  • Post sent a draft the composer never showed.
  • An upload reported its busy state to a host callback that never committed. A pick was refused as if a post were in flight.
  • An outside press dismissed a thread over an unsaved draft, with no warning.
  • A scroll gave the top edge back to the browser while the pull was still on.
  • Add built on a selection the list never showed.

Problem and solution fit

A write during render runs for every render, including the ones React throws away. Handlers and listeners run later, against the committed screen, so they read values from a screen nobody saw. The person acts on what is shown, and the action uses something else.

Where a handler owns the change, or a reader can take the value from its own render, the ref is gone. Otherwise a layout effect copies the value at commit, since no handler sees the change: eight refs, in six effects. Every reader keeps its current read.

Expected behavior and authority

  • React's useRef contract: do not write ref.current during rendering, except to initialize it.
  • architecture:react-component-runtime INV3 and INV4. Each new effect names what it syncs, acquires nothing, and can replay. No rendered value comes from a ref mutation.
  • usePanGesture got the same fix and the same suspended-transition test in fix(usePanGesture): write the onPan ref at commit #10090.

Smallest restoration

Twelve writes in seven files. The table under Testing gives each one's shape and reason. No API, visual or default changes. In TransferList, two render reads of the ref now read value, which that same render had just copied into it.

Evidence

  • Reproduction before the change: six new tests fail on main 738919b, each with the leaked value. For example, Post sent "Discarded draft".
  • Result after the change: the six pass.
  • Representative unchanged path: the 336 unit tests of Comments, RefreshControl, Schedule and TransferList pass, and so do their browser specs.
  • Regression test or other mutation-sensitive proof: each new test reads one ref, and it fails while that ref is written during render.

Scope

  • One defect is restored.
  • Separable contradictory or unsettled tagalongs were removed or split.
  • No new public API, visual representation, default, or interaction model is hidden under the bug-fix label.
  • Public text and artifacts contain no internal Meta context.

Testing

Each test renders the committed props, then re-renders new props in a transition that suspends for good. React keeps the committed tree and never commits that render. The test then acts on the committed screen.

File Ref Written now Why there Red-first test
CommentComposer.tsx latestValueRef Layout effect Mirrors the controlled value. The host also sets it without any handler here, as when it clears the draft after a post. Post sends the committed draft.
CommentComposer.tsx onActionPendingChangeRef Layout effect Mirrors a callback prop. Only the host's render changes it, and no handler here sees that. An upload reports to the committed callback.
CommentComposer.tsx isSubmittingRef Layout effect Mirrors the controlled isSubmitting. The host sets it as a post starts and ends, and no handler here sees that. A pick starts an upload while the committed composer is not submitting.
useCommentThreadDismissal.ts latestRef Layout effect Holds functions over draft, which the thread derives from its controller's state during render. No handler in this hook sees draft change. An outside press guards the committed draft.
refreshTouchController.ts isPullEnabledRef Layout effect Mirrors the isDisabled prop for the scroll listener. No handler here sees it change. A scroll keeps the edge claimed.
useRefreshControl.ts dispatchRef Layout effect Holds this hook's own dispatch callback, which no handler sets. No reachable failure. dispatch keeps one identity because its dependencies are all stable, so a discarded render wrote the same function. A probe over the hook's 66 tests saw one identity per hook.
TimeGridView.tsx positioning Layout effect, before its reader Mirrors props and the clock for the positioning layout effect, which must not re-run on clock ticks. No handler sees them change. No reachable failure. That effect reads only after a commit, and the committed render wrote last.
TimeGridView.tsx openBlockRef Removed: the handlers read openBlock Only this view's own handlers change and read it. No reachable failure. Those handlers run outside transitions, so a discarded render wrote the value the state already held.
TimeGridView.tsx popoverRef Removed: the close effect reads popover Its only reader is an effect with no dependency list, which runs after each commit with that commit's popover. No reachable failure. Passive effects run before React starts another render.
useScheduleViewPopover.ts openKeyRef Removed: the handlers read openKey Same as openBlockRef. Same as openBlockRef.
useScheduleViewPopover.ts popoverRef Removed: the effects read popover The close effect runs after each commit with that commit's popover. The frame callback needs only popover.contentRef, one useRef for the hook's life, now in that effect's dependencies. No reachable failure, for the same reasons.
TransferList.tsx currentValueRef Its change handler and a layout effect Mirrors the controlled value. commit, the list's own change handler, already writes it. The layout effect covers the host's own changes, as a reset, which no handler here sees. Add builds on the committed value.
  • pnpm exec vitest run packages/lab/src/{Comments,RefreshControl,Schedule,TransferList} --project ui: 16 files, 336 tests pass. On main, the six new tests fail.
  • RefreshControl interaction spec: Chromium 25 passed, 1 skipped; WebKit in the CI-profile container 20 passed, 6 skipped.
  • Schedule and TransferList a11y specs, Chromium: 52 passed.
  • @astryxdesign/lab typecheck, source and docs: 0 errors. ESLint on the changed files: no new findings. One existing warning in CommentComposer.test.tsx is on main too.

Seven Lab files copied props or state into refs during render. A render
React discards, as a suspended transition does, left its values there,
and the next event read them. Post sent a draft the composer never
showed, and an outside press dismissed a thread over an unsaved draft.
A scroll gave the pull's edge back to the browser, and Add built on a
selection the list never showed.

Where the handlers own the value, they now read it from their own
render and the ref is gone. Elsewhere a layout effect copies the value
at commit. Two TransferList render reads use `value` directly.
@vercel

vercel Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
astryx Ready Ready Preview Nov 9, 2026 1:08pm UTC

Request Review

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 9, 2026
@astracat-bot

astracat-bot Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

AI review status for this pull request.

Review status Updated
🟢 Reviewed (for maintainers only) Nov 9, 2026, 1:15 PM UTC

@github-actions

github-actions Bot commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

🔎 Preview · d0fa87b

Storybook · Sandbox · built from this PR's head

Modified components

Component Size LoC
lab/TransferList 101.7 kB (+10 B) · 1.2 kB CSS (0) —
lab/Comments 351.3 kB (+9 B) · 2.5 kB CSS (0) —
lab/CommentComposer 152.1 kB (+6 B) · 1.9 kB CSS (0) —
3 more components
Component Size LoC
lab/Schedule · Storybook 106.3 kB (0) · 3.9 kB CSS (0) 4,801 (−6)
lab/TransferListSelector 109.2 kB (0) · 1.4 kB CSS (0) —
lab/CommentThread 344.4 kB (−4 B) · 2.4 kB CSS (0) —

Package size: no published package changed; no new dependencies.

API changes: none in this pull request

Committed API report behind this build: cli 29 rows, core 36 rows, theme-butter 1 row, theme-y2k 1 row; run pnpm api:report.

Not run on this PR: add ci:run-visual, ci:run-a11y or ci:run-rtl to run them.


Generated by PR Enrichment workflow | View full report

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requesting changes for one small cleanup. The rest of the fix holds up: the six new tests fail against main and pass on this branch.

  • Drop the new popoverRef layout effects in TimeGridView.tsx (lines 268–272) and useScheduleViewPopover.ts (lines 92–96), as this PR already does for openBlockRef and openKeyRef, because every reader can use popover directly: the close effects run after every commit with that commit's popover, and the frame callback needs only the stable popover.contentRef.

[Automated review]

The close effects in TimeGridView and useScheduleViewPopover have no
dependency list, so each runs after a commit with that commit's
popover. The frame callback needs only popover.contentRef, one ref
object for the hook's life. So the popover refs and the layout effects
that copied them are gone.
@cixzhang cixzhang added the ci:run A maintainer lets this outside contributor's pull request run CI (AST-030 FR17) label Nov 9, 2026
@cixzhang

cixzhang commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

/approve-spec d0fa87b

@astracat-bot astracat-bot Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving: the popoverRef cleanup from the last review is in, and the six new tests fail against main and pass on this branch.

Approved pending CI: CI has not run on this head yet because the pull request is still a draft, and it runs once the pull request is marked ready for review.

One optional comment-wording suggestion is left inline.

[Automated review]

Comment on lines +324 to +326
// through this ref, as the composer's own listeners do. The ref is written
// at commit: a render React discards, as a suspended transition does, must
// not leave its draft for the listeners.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking, missed last round: "The ref is written at commit" is passive, and the sentence carries two topics. Keeping only the reason:

// The document listeners run outside render and reach the latest draft
// through this ref, as the composer's own listeners do. A render React
// discards, as a suspended transition does, must not leave its draft for
// the listeners.

This branch was successfully deployed

1 active deployment
Preview — d0fa87b5 Deployed Nov 9, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci:run A maintainer lets this outside contributor's pull request run CI (AST-030 FR17) CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants