-
-
Notifications
You must be signed in to change notification settings - Fork 566
feat(form-core): delay onMount listeners until defaultValues are present #1954
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
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 detectedLatest commit: 08abbe7 The changes in this PR will be included in the next version bump. This PR includes changesets to release 13 packages
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 |
|
WIP, I submitted PR to check it in real projects using pkg.new |
|
| 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
Restored accidentally deleted test files. Verified FieldApi.ts registration logic. Added test case for async defaultValues behavior.
…-10209780506199529847 delay-onmounted-default
|
I think we have a bigger issue related to |

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).