Skip to content

[next.js] initialFormState and no fields rendered may cause state.values undefined #1923

@MartinCura

Description

@MartinCura

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:

)

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

  1. Clone https://github.com/MartinCura/tanstack-form-1923
  2. pnpm i && pnpm run dev
  3. 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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions