Skip to content

Calling FieldApi.handleChange(undefined) sets field to field's default value instead #1614

@LeCarbonator

Description

@LeCarbonator

Describe the bug

When calling field.handleChange(undefined) in a framework, it will revert the field's value back to the default value after 1 rerender. It is because the .update() method will overwrite undefined.

  • Since it occurs in FieldApi.update, this error does not happen in form-core, but in framework adapters.
  • Calling field.handleChange(null) works as expected.
  • If the default value is also undefined, the change is unnoticeable.

Your minimal, reproducible example

https://stackblitz.com/edit/vitejs-vite-wbyrntnr?file=src%2FApp.tsx

Steps to reproduce

  1. There are two checkboxes per type of form (AppForm, Form)
  2. Uncheck the first one to set the value to undefined. It will revert to the default value instead of undefined.
  3. Uncheck the second one to set the value to null. It properly sets the value to null.

Expected behavior

As a user, I expect to be able to use undefined as an override value regardless of what the default Value is. Use cases include:

  • Libraries such as react-day-picker which use undefined instead of null as their state
  • Personal implementations that use undefined, such as optional select menus.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

It's related to the library itself, so the browser or framework has no impact.

TanStack Form adapter

None

TanStack Form version

v1.14.1

TypeScript version

v5.8.3

Additional context

Unit tests for adapters are lackluster. Simply removing the 'faulty' block on its own will not do. Make sure you don't accidentally break something else.

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