Skip to content

Commit c07c252

Browse files
fix(react): derive form state from submission outcomes and guard concurrent submits
- Represent useForm submission results with a discriminated union (SubmissionOutcome) owned by useActionState, deriving errors, error, hasChanges, wasSuccessful, and recentlySuccessful without redundant state or refs. - Snapshot form data and dispatchData at dispatch time so synchronous setData() calls, in-flight edits (including under resetOnSuccess), and queued React actions preserve their submitted payload and post-success baseline. - Scope callbackOutcomeRef across async onError and synchronous onSuccess so reset(), setError(), clearErrors(), and clearError() inside callbacks operate on the incoming outcome without leaking uncommitted defaults to external UI interactions during await. - Return a fresh idle outcome when a submission aborts so previous validation errors or success states do not revive. - Represent useIslandForm state with a single IslandOutcome union and requestId so the latest started submission owns pending and UI state across completion orders. - Check 429 retryAt deadlines directly in <Form> onSubmit instead of mounting timer state and effects.
1 parent 76d1a8a commit c07c252

4 files changed

Lines changed: 702 additions & 193 deletions

File tree

‎packages/react/src/runtime/form.tsx‎

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {
2-
useEffect,
3-
useState,
2+
useCallback,
3+
type FormEvent,
44
type FormHTMLAttributes,
55
type ReactElement,
66
type ReactNode,
@@ -9,7 +9,6 @@ import {
99
formErrorMessage,
1010
formRetryMessage,
1111
formSubmissionBlocked,
12-
scheduleFormRetry,
1312
type FormModel,
1413
type FormMethod,
1514
type VoidActionError,
@@ -31,22 +30,18 @@ export function Form({
3130
children,
3231
...props
3332
}: FormProps): ReactElement {
34-
const [now, setNow] = useState(Date.now);
35-
useEffect(() => scheduleFormRetry(form.error, () => setNow(Date.now())), [form.error]);
3633
const message = formErrorMessage(form);
3734
const retry = formRetryMessage(form.error);
38-
const blocked = formSubmissionBlocked(form, now);
35+
const onSubmit = useCallback(
36+
(event: FormEvent<HTMLFormElement>) => {
37+
if (formSubmissionBlocked(form)) {
38+
event.preventDefault();
39+
}
40+
},
41+
[form],
42+
);
3943
return (
40-
<form
41-
{...props}
42-
action={form[method]}
43-
aria-busy={form.pending}
44-
onSubmit={(event) => {
45-
if (blocked) {
46-
event.preventDefault();
47-
}
48-
}}
49-
>
44+
<form {...props} action={form[method]} aria-busy={form.pending} onSubmit={onSubmit}>
5045
{message && (
5146
<div role="alert" data-void-form-error>
5247
{form.error && renderError ? renderError(form.error) : message}

0 commit comments

Comments
 (0)