TanStack / form

🤖 Powerful and type-safe form state management for the web. TS/JS, React Form, Solid Form, Lit Form and Vue Form.

Home Page:https://tanstack.com/form

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TypeScript errors and React setState console warning when adding an item to the array field

SergiuPlesco opened this issue · comments

Describe the bug

The bug comes form TypeScript in the form of red lines due to the type definitions mismatch and second one in the console from React: " Warning: Cannot update a component (Field) while rendering a different component (Field). To locate the bad setState() call inside Field, follow the stack trace as described...."

Your minimal, reproducible example

https://codesandbox.io/p/devbox/react-form-2n6vht

Steps to reproduce

  1. open console
  2. click Add new to do button
  3. see screenshots

Expected behavior

No errors or warnings in the console or files

How often does this bug happen?

Every time

Screenshots or Videos

image image image

Platform

macbook pro, m1
nextjs 14

Tanstack Form adapter

react-form

TanStack Form version

0.13.6

TypeScript version

5

Additional context

No response

Confirming this issue. If you have default values, they default to undefined and updating them gives you

component is changing an uncontrolled input to be controlled. This is likely caused by the value changing from undefined to a defined value, which should not happen. Decide between using a controlled or uncontrolled input element for the lifetime of the component.

The validation works just fine on each individual input, but the data is not available to the form provider

So I was getting all kinds of errors by following what typescript wanted me to do with index and name. turns out, everything works when I ignore typescript and follow the docs. The issue is that my form field's TParentData does not extend any[]. I'm not sure why, since it's definitely an array.

} & (TParentData extends any[] ? {
    name?: TName;
    index: number;
} : {
    name: TName;
    index?: never;
})

So the code should look like it does in the docs:

<hobbiesField.Field
                  index={i}
                  name="description"

but ts was telling me to do this:

<hobbiesField.Field
                  // index={i}
                  name={`hobby.${i}.description`}

@SergiuPlesco please split this issue up into smaller byte sized chunks. There are some concerns here around the default warning (which should be fixed here: #606), while other comments are flagging challenges with the array API (fixing here: #588)