Skip to content
Source
Sign inSign up
TanStack/form

Field value within a re-indexed parent array item (keyed by UUID) temporarily becomes `undefined` on preceding item removal. #1518

Send to my agent
Closedrishitells(GitHub profile, opens in a new tab) opened this issue 5 comments

Describe the bug

In TanStack Form, when managing an array of objects where each object uses a unique React key (e.g., a uuid), a specific re-render issue can occur. If an item is removed from this array, and that item is not the last one, fields within the item that shifts to take the removed item's place can have their form.Field instance report state.value as undefined for a transient period during the re-render.

This behavior is unexpected because fields, especially those initialized with default values or specific types (like strings or other arrays), are generally assumed to consistently provide a value of that type, not undefined.

Your minimal, reproducible example

https://stackblitz.com/edit/tanstack-form-7wb235yh?file=src%2Fcollapsible-item.tsx

Steps to reproduce

  1. Setup: The example form contains a sections array. Each item in sections is an object with a unique uuid (used as its React key) and a sectionTitle string field (initialized with a default string). An invariant checks if sectionTitle's value is a string.
  2. Add Items: Click "Add Section" twice to create two distinct section items in the form.
  3. Remove Preceding Item: Click "Remove Section" for the first of the two section items.
  4. Observe Error: The application will throw an error, caught by the Error Boundary, for the remaining section item (which was previously the second). The error message will indicate that the sectionTitle field's value was undefined, failing the invariant check: Invariant failed: Expected sectionTitle field value to be a string! Received: undefined. (See attached screenshot).

Expected behavior

The state.value of any field (e.g., sectionTitle) within an array item should consistently maintain its correct type and initialized value throughout all re-render cycles. This includes re-renders that occur after removing a preceding item from the parent array and the subsequent re-indexing of items. A field initialized as a string should not temporarily become undefined.

How often does this bug happen?

Every time

Screenshots or Videos

https://github.com/user-attachments/assets/8fd533a5-2ab6-4d78-a11f-a6db69a1980a

Platform

  • Macos, Chrome browser

TanStack Form adapter

react-form

TanStack Form version

1.11.1

TypeScript version

No response

Additional context

No response

  1. Hi 🙂

    Could it be the same issue reported by @juanvilladev in https://github.com/TanStack/form/issues/1363 / https://github.com/TanStack/form/issues/1439 and possibly fixed by https://github.com/TanStack/form/pull/1440?

    I am seeing the same issue on my end as well!

  2. The issue persists in @tanstack/react-form version 1.23.7.

  3. Plus one for this. I found this issue in 1.14.1 and it persists in 1.27.6. Very strange behavior.

    Calling form.getFieldValue with the form.Field's name property value returns the correct result while the field.state.value is undefined.

  4. I found this issue in '@tanstack/react-form': ^1.28.3 which uses @tanstack/form-core@1.28.5.

    To reproduce it;

    • create a basic form with an object array.
    • create "add new item" button to add items to it.
    • create basic validation for the object's fields (like firstname is required, age must be a number etc.)
    • create a "remove" button for each object that uses form.removeFieldValue
    • Create an object fill it.
    • Create another one leave it empty.
    • Create the last one and fill it.
    • Remove the 2nd one (the one with the empty fields).

    Result:

    • No validation errors appear on the last card(it was the 3rd one before) when you submit it.
    • You cannot submit the form its invalid but not visible.
    • A newly created item will have the previous item's values for some reason.

    How to get rid of it for now

    • Use mapped array's index for the key property of the object jsx tag.
  5. Hi sorry it took so long for a response, but yeah you need to use the index of the map as the key for the container in order to get react reconciliation algorithm to react to changes.

    I'll add a note to the docs now explaining this.

    #2118

Sign in to join this conversation.