[PasswordToggleField] Controlled `visible` doesn't reset on form submit/reset with React 19.2.x
Bug report
Current Behavior
With a controlled PasswordToggleField (visible + onVisibilityChange) inside a form on React 19.2.x, the password stays visible after the user reveals it and then submits or resets the form (form.reset()). The input stays type="text" and onVisibilityChange(false) is never called.
After a reset, the field is cleared but stays type="text", so the next password typed is also shown in plain text.
PasswordToggleField.Input listens for the form's submit and reset events and calls setVisible(false) through useEffectEvent. The input is a forwardRef component, and on React 19.2.x the native useEffectEvent does not update its callback inside forwardRef components. The listeners therefore keep calling the setVisible from the first render. That function still compares against the initial visible value (false), so it treats the call as a no-op and never calls onVisibilityChange.
Uncontrolled usage (defaultVisible) isn't affected.
Expected behavior
Submitting or resetting the form hides the password again: onVisibilityChange(false) is called and the input goes back to type="password".
The same code behaves as expected on React 19.3.0.
Reproducible example
StackBlitz (React 19.2.8, radix-ui 1.6.7):
https://stackblitz.com/edit/raxw9yhq?file=src%2FApp.tsx
import * as React from 'react';
import { unstable_PasswordToggleField as PasswordToggleField } from 'radix-ui';
export default function App() {
const [visible, setVisible] = React.useState(false);
return (
<form onSubmit={(event) => event.preventDefault()}>
<PasswordToggleField.Root visible={visible} onVisibilityChange={setVisible}>
<PasswordToggleField.Input name="password" />
<PasswordToggleField.Toggle>Toggle</PasswordToggleField.Toggle>
</PasswordToggleField.Root>
<p>visible: {String(visible)}</p>
<button type="submit">Submit</button>
<button type="reset">Reset</button>
</form>
);
}- Type a password and click "Toggle". The password is shown and
visible: true. - Click "Submit" or "Reset".
- On React 19.2.8, the password stays visible (
type="text") andvisiblestaystrue. - On React 19.3.0 (change
reactandreact-dominpackage.json), the password is hidden andvisiblebecomesfalse.
Suggested solution
This is the same useEffectEvent-in-forwardRef problem as #4014, which #4015 worked around in DismissableLayer by using useCallbackRef instead of useEffectEvent.
The same workaround appears applicable here: wrap setVisible with useCallbackRef in PasswordToggleField.Input.
Additional context
- Upstream React issue: https://github.com/react/react/issues/34818
- Fixed by: https://github.com/react/react/pull/34831
- The fix was released in React 19.3.0.
- It was not backported to React 19.2.x: https://github.com/react/react/issues/35034
- The underlying
useEffectEventissue reproduces across React 19.2.0–19.2.8. - I reproduced the
PasswordToggleFieldbehavior in Chromium with React 19.2.8. The same code works as expected with React 19.3.0.
Your environment
| Software | Name(s) | Version |
|---|---|---|
| Radix Package(s) | radix-ui (@radix-ui/react-password-toggle-field) |
1.6.7 (0.1.11) |
| React | react, react-dom | 19.2.8 |
| Browser | Chromium | 152.0.7977.130 |
| Assistive tech | n/a | |
| Node | n/a | |
| npm/yarn/pnpm | npm | 10.9.8 |
| Operating System | macOS | 26.6.2 |
Source: radix-ui/primitives