-
Notifications
You must be signed in to change notification settings - Fork 50k
Description
React version: 19.1
When a form with an action is submitted, the form automatically resets. However, the form's onReset event is not triggered. The native 'reset' event is triggered though. Ideally either both would fire or not fire, not sure which is the more correct behavior.
I think this is because the reset occurs during the commit phase, during which all react event listeners are paused.
| ReactBrowserEventEmitterSetEnabled(false); |
Btw, another issue with triggering reset during commit is that it happens before effects have run, which means if components do handle reset events (natively), any effects that need the new defaultValue won't have run yet. You can work around this with useInsertionEffect but it's not obvious necessarily.
Steps To Reproduce
- Open https://stackblitz.com/edit/vitejs-vite-hjwn8je5?file=src%2FApp.tsx and open dev tools.
- Fill out the form and press the submit button.
- See that only the native 'reset' event is fired. The onReset prop is not called.
- When pressing the reset button, both native event and prop are called.
The current behavior
When automatic form reset happens, only the native 'reset' event is emitted.
The expected behavior
The onReset event should be called, or the native event should not be emitted. Either way, they should match their behavior.
Context
I'm working on fixing an issue in React Aria (adobe/react-spectrum#6830), which currently relies on native 'reset' events to clear validation errors when a form is reset by the user. However, with the automatic form reset after submit (and after the next render), validation errors that come back from the server after submitting are being cleared due to the reset.
Ideally there would be a way to distinguish between resets performed by a user and resets that react performs as part of a form submission. That way we could clear the validation errors when manually resetting a form, but preserve the new errors that are returned by an action.
Either way, I don't want to rely on the current behavior of onReset not being called during a submission if it is actually a bug.