Skip to content

fix: prevent initialFormState from overriding client values during merge#2096

Open
Mishra-coder wants to merge 1 commit intoTanStack:mainfrom
Mishra-coder:fix/issue-2089-initial-form-state-override
Open

fix: prevent initialFormState from overriding client values during merge#2096
Mishra-coder wants to merge 1 commit intoTanStack:mainfrom
Mishra-coder:fix/issue-2089-initial-form-state-override

Conversation

@Mishra-coder
Copy link
Copy Markdown

@Mishra-coder Mishra-coder commented Mar 26, 2026

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: undefined explicitly overrides client state
  • omitting values preserves 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
    • Optimized form state initialization by streamlining type handling while maintaining type compatibility across multiple packages.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 26, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 289da7f6-415d-4b8b-be30-69f6fbaa93bd

📥 Commits

Reviewing files that changed from the base of the PR and between e21cc01 and d6a7af9.

📒 Files selected for processing (2)
  • packages/react-form-nextjs/src/createServerValidate.ts
  • packages/react-form-remix/src/createServerValidate.ts

📝 Walkthrough

Walkthrough

The initialFormState constant in server validation modules (react-form-nextjs and react-form-remix) no longer explicitly includes values: undefined in the object literal. Instead, type assertions (as unknown as ServerFormState<any, undefined>) preserve type compatibility while removing the property from the runtime object shape.

Changes

Cohort / File(s) Summary
Server Form State Initialization
packages/react-form-nextjs/src/createServerValidate.ts, packages/react-form-remix/src/createServerValidate.ts
Removed explicit values: undefined property from initialFormState runtime object and added type assertion to maintain ServerFormState type compatibility across both packages.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

🐰 A rabbit hops through undefined lands,
Casting spells with careful hands,
Values vanish from the shape,
Type assertions help escape,
Two paths cleaned with one swift stride,
Runtime and types harmonized.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Description check ❓ Inconclusive The description provides a clear explanation of the problem, the solution, and the reasoning, but the PR description template sections (🎯 Changes checklist and 🚀 Release Impact) are not filled out. Complete the PR description template by checking off the applicable checklist items and specifying whether a changeset was generated for this release-impacting change.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and specifically describes the main fix: preventing initialFormState from overriding client values during merge, which directly addresses the core issue.
Linked Issues check ✅ Passed The changes directly address the root cause in issue #2089: removing the values property prevents initialFormState from overwriting client values during merge, resolving the undefined field values bug.
Out of Scope Changes check ✅ Passed All changes are scoped to the fix objective: modifications to initialFormState in both react-form-nextjs and react-form-remix adapters to address the issue without introducing unrelated changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

Comment @coderabbitai help to get the list of available commands and usage tips.

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.

Using next.js server actions, fields are set to undefined

1 participant