Skip to content

fix(otp): stale dispatch in React 19.2#3831

Open
askides wants to merge 3 commits intoradix-ui:mainfrom
askides:ai/fix-otp-dispatch-stale-closure
Open

fix(otp): stale dispatch in React 19.2#3831
askides wants to merge 3 commits intoradix-ui:mainfrom
askides:ai/fix-otp-dispatch-stale-closure

Conversation

@askides
Copy link
Copy Markdown

@askides askides commented Mar 17, 2026

What & Why

The OTP field stopped working in React 19.2 because the dispatch function captured stale value and collection state via useEffectEvent. This caused typed digits to not persist and keyboard navigation to break. This replaces useEffectEvent with useCallback + a ref so dispatch always reads the latest value while keeping a stable function identity.

Changes

  • Replaced useEffectEvent with React.useCallback for the dispatch function in OneTimePasswordField
  • Added a latestValueRef so dispatch reads fresh value without needing it as a dependency (keeps dispatch identity stable, avoids cascading effect re-runs)
  • Removed the @radix-ui/react-use-effect-event import
  • Added tests for typing with auto-advance and arrow key navigation

How to Test

  1. pnpm dev → open Storybook
  2. Navigate to Components > OneTimePasswordField
  3. Type digits — they should persist and focus should auto-advance
  4. After typing a few digits, press ArrowLeft/ArrowRight — should navigate between filled inputs
  5. Backspace should clear and move focus back
  6. pnpm vitest run packages/react/one-time-password-field

Closes #3709

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Mar 17, 2026

🦋 Changeset detected

Latest commit: 2f7959c

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

This PR includes changesets to release 2 packages
Name Type
@radix-ui/react-one-time-password-field Patch
radix-ui Patch

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

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.

Radix One-Time Password Field - Values Not Persisting and Keyboard Navigation Broken

1 participant