Skip to content

fix(react): derive form state from submission outcomes and guard concurrent submits - #738

Merged
cpojer merged 2 commits into
voidzero-dev:mainfrom
kotarotaniguchi0523:refactor/react-form-state
Nov 7, 2026
Merged

cpojer merged 2 commits into
voidzero-dev:mainfrom
kotarotaniguchi0523:refactor/react-form-state

Conversation

@kotarotaniguchi0523

Copy link
Copy Markdown
Contributor

Summary

  • Derive useForm() status (errors, error, hasChanges, wasSuccessful, recentlySuccessful) from the useActionState outcome instead of syncing separate states and refs across await.
  • Snapshot form data at dispatch time so queued React actions and in-flight edits (including with resetOnSuccess: true) keep their submitted payload and baseline.
  • Clear previous errors and success state when a submission starts, without reviving them if the submission aborts.
  • Ensure reset(), setError(), clearErrors(), and clearError() inside onSuccess / onError apply to the new outcome without exposing uncommitted defaults during await.
  • Guard useIslandForm() with a request ID so the latest submission owns pending and UI state across out-of-order completions.
  • Check 429 retryAt deadlines directly in <Form> onSubmit instead of mounting a timer effect.

Test plan

  • vp fmt --check & vp lint
  • vp run build & vp run check:sdk
  • vp test run packages/react/test/unit

kotarotaniguchi0523 and others added 2 commits November 6, 2026 17:41
…urrent 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.
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.

@cpojer cpojer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@cpojer
cpojer merged commit 15e12e8 into voidzero-dev:main Nov 7, 2026
2 checks passed
@kotarotaniguchi0523
kotarotaniguchi0523 deleted the refactor/react-form-state branch November 8, 2026 05:41
yangshun added a commit to yangshun/void that referenced this pull request Nov 9, 2026
…olid

A second submit aborts the first through the router, and the first's
cleanup then cleared `pending` while the second was still running. An older
submit that still completed could also apply its errors, success state and
defaults. Island forms have no abort, so an older response could overwrite a
newer result, reload the page or redirect.

Only the latest submit now updates form state or changes the page, matching
the request ID guard React's useIslandForm gained in voidzero-dev#738. An older submit's
real failure still rejects its own promise.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
yangshun added a commit to yangshun/void that referenced this pull request Nov 9, 2026
…d Solid

On success these forms saved their values as of the response, so anything
typed while the request was in flight counted as saved: `hasChanges` read
false and `reset()` restored text that was never submitted. `resetOnSuccess`
also wiped those edits, and page forms sent a shallow copy that shared nested
values with the live form.

Each form now takes a deep copy when submit starts, sends it and saves it as
the new defaults. `resetOnSuccess` only clears the data if it has not changed
since submit. This matches what React's forms have done since voidzero-dev#738.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
cpojer pushed a commit that referenced this pull request Nov 9, 2026
)

## Summary

Brings `useForm` and `useIslandForm` in Vue, Svelte and Solid in line
with React, fixing two bugs in how they handle a submit that is still in
flight.

## Why the adapters differ

All four adapters used to share these bugs.
[#738](#738) fixed them in
React only, so React has guarded concurrent submits and kept submitted
values since then, while Vue, Svelte and Solid have not.

## The issues

**1. An older submit could still change the form when a newer one was
running** (`3c9ddc5ae`)

The forms tracked submits with flags and did not record which one was
latest.

- **Page forms:** a second submit aborts the first through the router,
and the first's `finally` then set `pending = false` while the second
was still running. A submit button disabled by `pending` became
clickable again mid-request. An older submit that still completed could
also apply its errors, success state and defaults.
- **Island forms:** nothing aborts the older request, so its response
could overwrite a newer result, clear `pending`, reload the page or
redirect, whichever finished first.

**2. Edits made during a submit counted as saved** (`e4ca5a713`)

On success, the forms saved their values as of the response rather than
as of the submit.

- `hasChanges` read `false`, and `reset()` restored text that was never
sent.
- `resetOnSuccess` wiped anything typed while the request was in flight.
- Page forms sent a shallow copy, so nested values were shared with the
live form.

## The fix

- Each form counts its submits with `requestId` and `latestRequestId`,
the same names React's island form uses. Only the latest submit updates
state, reloads or redirects. An older submit's real failure still
rejects its own promise.
- Each form takes a deep copy when submit starts, sends it and saves it
as the new defaults.
- `resetOnSuccess` only clears the data if it has not changed since
submit.
- The `hasChanges` and `resetOnSuccess` docs now cover edits made during
a submit. Most of that diff is the formatter repadding the table.

## Behaviour that still differs from React

- **Two page-form submits:** React's `useActionState` queues them, while
Vue, Svelte and Solid abort the first and keep the latest. This PR keeps
that difference. The server may still have processed an aborted request.
- **`resetOnSuccess` edit check:** these adapters compare values,
because their form data is mutated in place, while React compares object
identity. Typing something and changing it back counts as unchanged
here.

## Test plan

New tests cover both finishing orders, aborts, older failures, in-flight
edits and `resetOnSuccess`, based on the React test added in #738.
Solid's are in a new file because they need its client runtime. They
assert through `reset()` rather than `hasChanges`, because `createStore`
and `createMemo` do not track each other under those test mocks.

- [x] All new tests that assert the bugs failed before their fix (18 for
overlap, 9 for in-flight edits)
- [x] Form tests for all four adapters pass (72)
- [x] Vue, Svelte and Solid typechecks, format and lint pass
- [x] `vp run check` passes (33/33)
- [x] CI passes on `e4ca5a713`
- [x] Local browser tests on the rebased code. `vp test run` had 16,106
tests pass and 41 fail, every failure being `browserType.launch:
Executable doesn't exist`, because this machine lacks the Chromium build
Playwright 1.65 expects

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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.

2 participants