[next.js] `initialFormState` and no fields rendered may cause `state.values` undefined
Nobody has claimed this yet.
- Dominant language
- TypeScript
- Stars
- 6.7k
- Forks
- 682
- Avg merge
- 5d 18h
- Merged PRs (30d)
- 7
Description
Describe the bug
Hi! Great library you have here. I've been using it for while now and first time i've come across this.
In the Next.js App Router guide (https://tanstack.com/form/latest/docs/framework/react/guides/ssr#app-router-integration) it recommends to set the initial server state with
import { initialFormState } from '@tanstack/react-form-nextjs'
// inside component:
const [formState, action] = useActionState(someAction, initialFormState)
// ...
const form = useForm({
transform: useTransform((baseForm) => mergeForm(baseForm, formState ?? {}), [formState]),
// ...
but initialFormState as defined has values: undefined (see: https://github.com/TanStack/form/blob/6a5e1c1a4f0f0519705cc5fd15cbe8afb878a42d/packages/react-form-nextjs/src/createServerValidate.ts#L133)
This means that trying to access state.values.<field> will break as values is now undefined. In my case this cropped up trying to use <form.Subscribe>
Your minimal, reproducible example
None, sorry
Steps to reproduce
- Clone https://github.com/MartinCura/tanstack-form-1923
- pnpm i && pnpm run dev
- Go to http://localhost:3000, reload page
Expected behavior
I expect values to always be defined, as should all of the fields in my schema.
The bug is in application code! ...but following the docs' recommendations 🤔, and using initialFormState which introduces the issue.
How often does this bug happen?
Often
Screenshots or Videos
No response
Platform
- Fedora
- Firefox / Zen
TanStack Form adapter
None
TanStack Form version
v1.27.1
TypeScript version
v5.9.3
Additional context
I'm not sure if this is a docs issue instead of any type of bug, i'll leave it up to you!
I'd say the bottom line is not sure why the values should be allowed to be undefined, and thus if either initialFormState should be something that adapts to the form schema, or outright don't recommend it and make the user write it manually, maybe copying the default values (which is more or less the fix i implemented).
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with packages/react-form-nextjs/src/createServerValidate.ts around the initialFormState definition and read the Next.js App Router integration guide. Reproduce the behavior with the linked tanstack-form-1923 example, then determine whether the implementation or documentation should change. Done means the documented setup no longer leads to state.values being undefined, with the expected behavior made explicit.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- next.js, react, typescript
- Domain
- frontend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100