fix: prevent initialFormState from overriding client values during merge #2096
main fromrefs/pull/2096/head·opened The initialFormState exported by the Next.js and Remix adapters contained a values: undefined property. During the server-client state merge process (specifically in mutateMergeDeep), this undefined value was being merged into the client's form state, causing existing field values to be reset to undefined after initial mount or during route transitions.
This fix removes the values property from the initialFormState object in both adapters. By omitting the property entirely, the merge logic no longer overwrites existing client values when no server-side values are provided.
This approach is safe because:
values: undefinedexplicitly overrides client state- omitting
valuespreserves existing client values - server-provided
values(e.g. after validation) will still correctly override client state as expected
To maintain type compatibility, a TypeScript cast is used:
as unknown as ServerFormState<any, undefined>
This resolves the issue where forms using server actions would have their fields set to undefined after initial render or during client-side navigation.
Closes #2089
Summary by CodeRabbit#
- Refactor
- Streamlined form state initialization and type handling across packages to simplify runtime shape while preserving compatibility.
- Tests
- Added test suites verifying initial server form state omits client values and that merging server state into client state does not overwrite existing form values.
- 1
View your CI Pipeline Execution ↗ for commit d6a7af9e306aca34c88e899dc596df811e09bf09
Command Status Duration Result nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded 1m 2s View ↗ nx run-many --target=build --exclude=examples/**✅ Succeeded 6s View ↗
☁️ Nx Cloud last updated this comment at
2026-03-29 09:51:05UTCMore templates
- @tanstack/form-example-angular-array
- @tanstack/form-example-angular-large-form
- @tanstack/form-example-angular-simple
- @tanstack/form-example-angular-standard-schema
- @tanstack/form-example-lit-array
- @tanstack/form-example-lit-simple
- @tanstack/form-example-lit-standard-schema
- @tanstack/form-example-lit-ui-libraries
- @tanstack/form-example-react-array
- @tanstack/form-example-react-compiler
- @tanstack/form-example-react-devtools
- @tanstack/form-example-react-dynamic
- @tanstack/field-errors-from-form-validators
- @tanstack/form-example-react-large-form
- @tanstack/form-example-react-next-server-actions
- @tanstack/form-example-react-next-server-actions-zod
- @tanstack/form-example-react-query-integration
- @tanstack/form-example-remix
- @tanstack/form-example-react-simple
- @tanstack/form-example-react-standard-schema
- @tanstack/form-example-react-tanstack-start
- @tanstack/form-example-react-ui-libraries
- @tanstack/form-example-solid-array
- @tanstack/form-example-solid-devtools
- @tanstack/form-example-solid-large-form
- @tanstack/form-example-solid-simple
- @tanstack/form-example-solid-standard-schema
- @tanstack/form-example-svelte-array
- @tanstack/form-example-svelte-large-form
- @tanstack/form-example-svelte-simple
- @tanstack/form-example-svelte-standard-schema
- @tanstack/form-example-vue-array
- @tanstack/form-example-vue-simple
- @tanstack/form-example-vue-standard-schema
@tanstack/angular-form
npm i https://pkg.pr.new/@tanstack/angular-form@2096@tanstack/form-core
npm i https://pkg.pr.new/@tanstack/form-core@2096@tanstack/form-devtools
npm i https://pkg.pr.new/@tanstack/form-devtools@2096@tanstack/lit-form
npm i https://pkg.pr.new/@tanstack/lit-form@2096@tanstack/react-form
npm i https://pkg.pr.new/@tanstack/react-form@2096@tanstack/react-form-devtools
npm i https://pkg.pr.new/@tanstack/react-form-devtools@2096@tanstack/react-form-nextjs
npm i https://pkg.pr.new/@tanstack/react-form-nextjs@2096@tanstack/react-form-remix
npm i https://pkg.pr.new/@tanstack/react-form-remix@2096@tanstack/react-form-start
npm i https://pkg.pr.new/@tanstack/react-form-start@2096@tanstack/solid-form
npm i https://pkg.pr.new/@tanstack/solid-form@2096@tanstack/solid-form-devtools
npm i https://pkg.pr.new/@tanstack/solid-form-devtools@2096@tanstack/svelte-form
npm i https://pkg.pr.new/@tanstack/svelte-form@2096@tanstack/vue-form
npm i https://pkg.pr.new/@tanstack/vue-form@2096commit:
d6a7af9Codecov Report#
:white_check_mark: All modified and coverable lines are covered by tests. :white_check_mark: Project coverage is 0.00%. Comparing base (
6892ed0) to head (d6a7af9). :warning: Report is 159 commits behind head on main.Additional details and impacted files
@@ Coverage Diff @@ ## main #2096 +/- ## ========================================== - Coverage 90.35% 0.00% -90.36% ========================================== Files 38 6 -32 Lines 1752 50 -1702 Branches 444 18 -426 ========================================== - Hits 1583 0 -1583 + Misses 149 42 -107 + Partials 20 8 -12:umbrella: View full report in Codecov by Sentry. :loudspeaker: Have feedback on the report? Share it here.
:rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
- :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed. Inline comments: In `@packages/react-form-nextjs/tests/createServerValidate.spec.tsx`: - Around line 17-18: Add a descriptive reason to the existing TypeScript suppression above the mutateMergeDeep(clientState, initialFormState) call: replace the bare "// `@ts-ignore`" with a comment that includes the TS error code and a short justification (e.g., indicating this is intentional test-only mismatch of types), or alternatively use "// eslint-disable-next-line `@typescript-eslint/ban-ts-comment` -- <reason>" style; ensure the comment directly precedes the mutateMergeDeep invocation so ESLint recognizes the description. In `@packages/react-form-remix/tests/createServerValidate.spec.tsx`: - Around line 17-18: Replace the bare "// `@ts-ignore`" above the call to mutateMergeDeep(clientState, initialFormState) with a documented suppression or preferred directive: either use "// `@ts-expect-error` -- <reason why types are incompatible at runtime>" to ensure the suppression fails if types become compatible, or keep "// `@ts-ignore` -- <explicit reason>" explaining why the type error is acceptable here; make sure the comment references the mutateMergeDeep call and why clientState/initialFormState type mismatch is intentionally ignored.🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID:
8398ee9b-ed6b-43a1-8cd2-af200314aca2📥 Commits
Reviewing files that changed from the base of the PR and between d6a7af9e306aca34c88e899dc596df811e09bf09 and 4d8e0abf2740fea8c6a2245479a4845a8ab8abb7.
📒 Files selected for processing (2)
packages/react-form-nextjs/tests/createServerValidate.spec.tsxpackages/react-form-remix/tests/createServerValidate.spec.tsx
packages/react-form-nextjs/tests/createServerValidate.spec.tsx lines 17 to 18@@ -0,0 +1,18 @@1+import { describe, expect, it } from 'vitest'2+import { mutateMergeDeep } from '@tanstack/react-form'3+import { initialFormState } from '../src/createServerValidate'4+5+describe('initialFormState', () => {6+ it('should not contain values property', () => {7+ expect(initialFormState).not.toHaveProperty('values')8+ })9+10+ it('should not override client values when merged', () => {11+ const clientState = {12+ values: { name: 'client-name' },13+ errors: [],14+ errorMap: {}15+ }16+17+ // @ts-ignore18+ mutateMergeDeep(clientState, initialFormState)⚠️ Potential issue | 🟡 Minor
Add description to
@ts-ignoredirective.Same issue as in the Remix test file. ESLint requires a description for the type suppression.
Proposed fix
- // `@ts-ignore` + // `@ts-expect-error` - Testing with simplified mock state that doesn't match full ServerFormState type mutateMergeDeep(clientState, initialFormState)📝 Committable suggestion
‼️ IMPORTANT Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
// `@ts-expect-error` - Testing with simplified mock state that doesn't match full ServerFormState type mutateMergeDeep(clientState, initialFormState)🧰 Tools
🪛 ESLint
[error] 17-17: Include a description after the "@ts-ignore" directive to explain why the
@ts-ignoreis necessary. The description must be 3 characters or longer.(
@typescript-eslint/ban-ts-comment)🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/react-form-nextjs/tests/createServerValidate.spec.tsx` around lines 17 - 18, Add a descriptive reason to the existing TypeScript suppression above the mutateMergeDeep(clientState, initialFormState) call: replace the bare "// `@ts-ignore`" with a comment that includes the TS error code and a short justification (e.g., indicating this is intentional test-only mismatch of types), or alternatively use "// eslint-disable-next-line `@typescript-eslint/ban-ts-comment` -- <reason>" style; ensure the comment directly precedes the mutateMergeDeep invocation so ESLint recognizes the description.packages/react-form-remix/tests/createServerValidate.spec.tsx lines 17 to 18@@ -0,0 +1,18 @@1+import { describe, expect, it } from 'vitest'2+import { mutateMergeDeep } from '@tanstack/react-form'3+import { initialFormState } from '../src/createServerValidate'4+5+describe('initialFormState', () => {6+ it('should not contain values property', () => {7+ expect(initialFormState).not.toHaveProperty('values')8+ })9+10+ it('should not override client values when merged', () => {11+ const clientState = {12+ values: { name: 'client-name' },13+ errors: [],14+ errorMap: {}15+ }16+17+ // @ts-ignore18+ mutateMergeDeep(clientState, initialFormState)⚠️ Potential issue | 🟡 Minor
Add description to
@ts-ignoredirective.ESLint requires a description explaining why the type suppression is necessary. Consider using
@ts-expect-errorwhich is preferred as it will error if the suppression becomes unnecessary.Proposed fix
- // `@ts-ignore` + // `@ts-expect-error` - Testing with simplified mock state that doesn't match full ServerFormState type mutateMergeDeep(clientState, initialFormState)📝 Committable suggestion
‼️ IMPORTANT Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
// `@ts-expect-error` - Testing with simplified mock state that doesn't match full ServerFormState type mutateMergeDeep(clientState, initialFormState)🧰 Tools
🪛 ESLint
[error] 17-17: Include a description after the "@ts-ignore" directive to explain why the
@ts-ignoreis necessary. The description must be 3 characters or longer.(
@typescript-eslint/ban-ts-comment)🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@packages/react-form-remix/tests/createServerValidate.spec.tsx` around lines 17 - 18, Replace the bare "// `@ts-ignore`" above the call to mutateMergeDeep(clientState, initialFormState) with a documented suppression or preferred directive: either use "// `@ts-expect-error` -- <reason why types are incompatible at runtime>" to ensure the suppression fails if types become compatible, or keep "// `@ts-ignore` -- <explicit reason>" explaining why the type error is acceptable here; make sure the comment references the mutateMergeDeep call and why clientState/initialFormState type mismatch is intentionally ignored.Mishra-coder(GitHub profile, opens in a new tab) linked this to fix #2089
Sign in to join this conversation.
📝 Walkthrough
Walkthrough#
The
initialFormStatein both server-validation modules now omits the runtimevaluesproperty and uses a type assertion to satisfyServerFormState<any, undefined>. Added tests in each package verifyinginitialFormStatelacksvaluesand does not overwrite clientvalueswhen merged.Changes#
packages/react-form-nextjs/src/createServerValidate.ts,packages/react-form-remix/src/createServerValidate.tsvalues: undefinedfrom the runtime object; addedas unknown as ServerFormState<any, undefined>type assertion to preserve compile-time type.packages/react-form-nextjs/tests/createServerValidate.spec.tsx,packages/react-form-remix/tests/createServerValidate.spec.tsxinitialFormStatehas novalueskey and that deep-merging it into a client state does not overwrite existingclientState.values.Estimated code review effort#
🎯 3 (Moderate) | ⏱️ ~20 minutes
Poem#
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
#2089by removing thevaluesproperty frominitialFormStatein both Next.js and Remix adapters and adding regression tests.#2089: modifications to createServerValidate.ts files and corresponding regression tests in both adapters.✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share