Skip to content

Conversation

@ws-rush
Copy link

@ws-rush ws-rush commented Dec 25, 2025

This PR modifies the behavior of onMount listeners and validators in FormApi and FieldApi to better support async initial values.

Previously, onMount listeners and validators ran immediately upon mounting, regardless of whether defaultValues were available. This often led to premature validation errors or side effects when fetching data asynchronously.

With this change:

onMount listeners and validators (both at the form and field level) are delayed if defaultValues is undefined or null.
They will automatically execute once defaultValues becomes defined (e.g., when data finishes loading).
This allows developers to initialize useForm immediately but defer validation until data is ready, simplifying the handling of loading states.
Internal changes:

Added _hasMounted and _isMounted flags to FormApi and FieldApi to track initialization state and prevent duplicate execution.
Updated mount and update methods in both APIs to respect the presence of defaultValues.
Updated documentation with an example of handling async initial values.
✅ Checklist
I have followed the steps in the Contributing guide.
I have tested this code locally with pnpm test:pr.
🚀 Release Impact
This change affects published code, and I have generated a changeset.
This change is docs/CI/dev-only (no release).

google-labs-jules bot and others added 2 commits December 25, 2025 18:21
Delay the execution of `onMount` listeners and validators for both forms and fields until `defaultValues` are provided (not undefined or null). This allows for easier handling of async data loading without premature validation.

- Added `_hasMounted` and `_isMounted` flags to `FormApi` and `FieldApi`.
- Modified `FormApi.mount` and `FormApi.update` to check for `defaultValues`.
- Modified `FieldApi.mount` and `FieldApi.update` to check for form `defaultValues`.
- Updated documentation with an async example.
…-10209780506199529847

Delay onMount until defaultValues are present
@changeset-bot
Copy link

changeset-bot bot commented Dec 25, 2025

🦋 Changeset detected

Latest commit: 08abbe7

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 13 packages
Name Type
@tanstack/form-core Minor
@tanstack/angular-form Minor
@tanstack/form-devtools Patch
@tanstack/lit-form Patch
@tanstack/react-form Minor
@tanstack/solid-form Minor
@tanstack/svelte-form Minor
@tanstack/vue-form Minor
@tanstack/react-form-devtools Patch
@tanstack/solid-form-devtools Patch
@tanstack/react-form-nextjs Minor
@tanstack/react-form-remix Minor
@tanstack/react-form-start Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@ws-rush
Copy link
Author

ws-rush commented Dec 25, 2025

WIP, I submitted PR to check it in real projects using pkg.new

@nx-cloud
Copy link

nx-cloud bot commented Dec 25, 2025

🤖 Nx Cloud AI Fix Eligible

An automatically generated fix could have helped fix failing tasks for this run, but Self-healing CI is disabled for this workspace. Visit workspace settings to enable it and get automatic fixes in future runs.

To disable these notifications, a workspace admin can disable them in workspace settings.


View your CI Pipeline Execution ↗ for commit 576c77a

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ❌ Failed 2m 2s View ↗
nx run-many --target=build --exclude=examples/** ✅ Succeeded 34s View ↗

☁️ Nx Cloud last updated this comment at 2025-12-25 18:55:55 UTC

@pkg-pr-new
Copy link

pkg-pr-new bot commented Dec 25, 2025

More templates

@tanstack/angular-form

npm i https://pkg.pr.new/@tanstack/angular-form@1954

@tanstack/form-core

npm i https://pkg.pr.new/@tanstack/form-core@1954

@tanstack/form-devtools

npm i https://pkg.pr.new/@tanstack/form-devtools@1954

@tanstack/lit-form

npm i https://pkg.pr.new/@tanstack/lit-form@1954

@tanstack/react-form

npm i https://pkg.pr.new/@tanstack/react-form@1954

@tanstack/react-form-devtools

npm i https://pkg.pr.new/@tanstack/react-form-devtools@1954

@tanstack/react-form-nextjs

npm i https://pkg.pr.new/@tanstack/react-form-nextjs@1954

@tanstack/react-form-remix

npm i https://pkg.pr.new/@tanstack/react-form-remix@1954

@tanstack/react-form-start

npm i https://pkg.pr.new/@tanstack/react-form-start@1954

@tanstack/solid-form

npm i https://pkg.pr.new/@tanstack/solid-form@1954

@tanstack/solid-form-devtools

npm i https://pkg.pr.new/@tanstack/solid-form-devtools@1954

@tanstack/svelte-form

npm i https://pkg.pr.new/@tanstack/svelte-form@1954

@tanstack/vue-form

npm i https://pkg.pr.new/@tanstack/vue-form@1954

commit: 576c77a

google-labs-jules bot and others added 3 commits December 25, 2025 19:30
Restored accidentally deleted test files.
Verified FieldApi.ts registration logic.
Added test case for async defaultValues behavior.
…-10209780506199529847

delay-onmounted-default
@ws-rush
Copy link
Author

ws-rush commented Dec 25, 2025

I think we have a bigger issue related to onMount in versions above 126.0, I will report them after make stackblitz to ensure

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.

1 participant