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

Aperta
#1,923 10 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
next.js, react, typescript
Ambito
frontend

Direzione di ricerca

Inizia da packages/react-form-nextjs/src/createServerValidate.ts, intorno alla definizione di initialFormState, e leggi la guida all’integrazione di Next.js App Router. Riproduci il comportamento con l’esempio collegato di tanstack-form-1923, quindi determina se debba cambiare l’implementazione o la documentazione. Il lavoro è completato quando la configurazione documentata non fa più sì che state.values sia undefined e il comportamento previsto è reso esplicito.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

v1 v2: needs investigation
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
  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).

Lingua principale
TypeScript
Stelle
6.8k
Fork
717
Merge medio
5g 22h
PR unite (30g)
14

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di TanStack/form

Tutte le issue di TanStack/form

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.