Skip to content

Commit 6fbf3f1

Browse files
committed
Preserve React form state in async callbacks.
Keep the submission outcome available until onSuccess settles so reset and field errors still target the successful submission after await. Reset uses the accepted snapshot while the callback is pending and retains the previous baseline until the request succeeds.
1 parent c07c252 commit 6fbf3f1

2 files changed

Lines changed: 61 additions & 7 deletions

File tree

‎packages/react/src/runtime/use-form.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,15 @@ export function useForm(
155155
? cloneFormValue(initialDefaults)
156156
: cloneFormValue(payload.snapshot);
157157
const nextOutcome: SubmissionOutcome = { status: 'success', defaults: nextDefaults };
158-
let pendingSuccess: void | Promise<void>;
158+
// Helpers called after an await still belong to this successful submission.
159159
callbackOutcomeRef.current = nextOutcome;
160160
try {
161-
pendingSuccess = options?.onSuccess?.();
161+
await options?.onSuccess?.();
162162
} finally {
163-
callbackOutcomeRef.current = null;
163+
if (callbackOutcomeRef.current === nextOutcome) {
164+
callbackOutcomeRef.current = null;
165+
}
164166
}
165-
await pendingSuccess;
166167
if (options?.resetOnSuccess && dataRef.current === payload.dispatchData) {
167168
dataRef.current = cloneFormValue(initialDefaults);
168169
setDataState(dataRef.current);

‎packages/react/test/unit/use-form.test.ts‎

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,42 @@ describe('useForm callbacks', () => {
129129
expect(formRef.current!.wasSuccessful).toBe(true);
130130
});
131131

132+
it('resets to saved values after awaiting onSuccess work', async () => {
133+
const { formRef } = await mountForm(
134+
vi.fn(async () => ({})),
135+
{
136+
onSuccess: async () => {
137+
await Promise.resolve();
138+
formRef.current!.reset();
139+
},
140+
},
141+
);
142+
await act(async () => {
143+
formRef.current!.setData('title', 'Saved');
144+
});
145+
146+
await submitPost(formRef.current!);
147+
148+
expect(formRef.current!.data.title).toBe('Saved');
149+
expect(formRef.current!.hasChanges).toBe(false);
150+
});
151+
152+
it('preserves field errors set after awaiting onSuccess work', async () => {
153+
const { formRef } = await mountForm(
154+
vi.fn(async () => ({})),
155+
{
156+
onSuccess: async () => {
157+
await Promise.resolve();
158+
formRef.current!.setError('title', 'Check the saved value');
159+
},
160+
},
161+
);
162+
163+
await submitPost(formRef.current!);
164+
165+
expect(formRef.current!.errors).toEqual({ title: 'Check the saved value' });
166+
});
167+
132168
it('calls onError with validation errors before the updated UI commits', async () => {
133169
const onError = vi.fn(() => {
134170
expect(container.textContent).not.toBe('Required');
@@ -378,11 +414,15 @@ describe('useForm state derivation and error management', () => {
378414
expect(formRef.current!.hasChanges).toBe(false);
379415
});
380416

381-
it('applies reset(), setError(), and clearError() inside onSuccess/onError without leaking uncommitted outcomes to external UI during await', async () => {
417+
it('applies form helpers during callbacks and only resets to a new baseline after the request succeeds', async () => {
382418
let mode: 'success-reset' | 'async-external-reset' | 'validation' | 'action-error' =
383419
'success-reset';
420+
const requestGate = deferred<Record<string, unknown>>();
384421
const callbackGate = deferred<void>();
385422
const visit = vi.fn(async () => {
423+
if (mode === 'async-external-reset') {
424+
return requestGate.promise;
425+
}
386426
if (mode === 'validation') {
387427
return { errors: { title: 'Server invalid', extra: 'Keep me' } };
388428
}
@@ -424,20 +464,33 @@ describe('useForm state derivation and error management', () => {
424464
expect(formRef.current!.data).toEqual({ title: 'Saved' });
425465
expect(formRef.current!.hasChanges).toBe(false);
426466

427-
// While async onSuccess is suspended at await, external UI reset() uses committed baseline ('Saved'), not uncommitted ('Uncommitted')
467+
// Until the request succeeds, reset() still uses the previous saved values.
428468
mode = 'async-external-reset';
429469
await act(async () => {
430-
formRef.current!.setData('title', 'Uncommitted');
470+
formRef.current!.setData('title', 'Next saved');
431471
});
432472
await submitPost(formRef.current!);
433473
expect(formRef.current!.pending).toBe(true);
434474
await act(async () => {
435475
formRef.current!.reset();
436476
});
437477
expect(formRef.current!.data).toEqual({ title: 'Saved' });
478+
479+
// The server has accepted the new snapshot, even while onSuccess is still pending.
480+
await act(async () => {
481+
requestGate.resolve({});
482+
});
483+
expect(formRef.current!.pending).toBe(true);
484+
expect(formRef.current!.wasSuccessful).toBe(false);
485+
await act(async () => {
486+
formRef.current!.reset();
487+
});
488+
expect(formRef.current!.data).toEqual({ title: 'Next saved' });
438489
await act(async () => {
439490
callbackGate.resolve();
440491
});
492+
expect(formRef.current!.pending).toBe(false);
493+
expect(formRef.current!.hasChanges).toBe(false);
441494

442495
// onError -> clearErrors() + setError()
443496
mode = 'validation';

0 commit comments

Comments
 (0)