Skip to content
Source
Sign inSign up
TanStack/form

`isDefaultValue` is wrong when a field-level `defaultValue` is `null` #2427

Openmaricastroc(GitHub profile, opens in a new tab) opened this issue 1 comment

Describe the bug

When a field has defaultValue: null and the form-level default for that field is anything other than null (missing, undefined, or a different value), isDefaultValue does not match what form.reset() restores:

  • The field holds its default (null) but field.state.meta.isDefaultValue and form.state.isDefaultValue are false, right after mount and after form.reset().
  • Setting the value to the form-level default (or to undefined when there is none) reports isDefaultValue: true, although form.reset() then changes the value back to null.

Field-level defaults of 0, false, '' or any non-null value work as expected.

Your minimal, reproducible example

The snippet under "Steps to reproduce" is self-contained and only needs @tanstack/form-core.

Steps to reproduce

import { FieldApi, FormApi } from '@tanstack/form-core'

const form = new FormApi({
  defaultValues: { name: 'form' as string | null },
})
form.mount()

const field = new FieldApi({ form, name: 'name', defaultValue: null })
field.mount()

console.log(field.state.value, field.state.meta.isDefaultValue)
// null false   (expected: null true)

field.setValue('form')
console.log(field.state.meta.isDefaultValue)
// true         (expected: false)

form.reset()
console.log(field.state.value, field.state.meta.isDefaultValue)
// null false   (expected: null true)

Without a form-level default for the field (defaultValues: {} as { name?: string | null }), the result is the same, and field.setValue(undefined) reports isDefaultValue: true.

Expected behavior

isDefaultValue should be true exactly when the current value is the one form.reset() restores, as established in #1973 and #2006. With a field-level defaultValue: null that value is null.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

macOS, reproduced in Node with Vitest (no browser involved)

TanStack Form adapter

vanilla

TanStack Form version

v1.33.5

TypeScript version

v5.9.3

Additional context

The derivation resolves the default with fieldDefault ?? formDefault, so a field-level null falls through to the form-level default: https://github.com/TanStack/form/blob/2216fdedf0a5ff17e4ef8567e1321442639b96ca/packages/form-core/src/FormApi.ts#L1236-L1241

form.reset() and FieldApi treat a field-level default as set when it is !== undefined, so null is applied as a default there. This was introduced in #2006 (1.32.0) and noted in an unanswered review comment: https://github.com/TanStack/form/pull/2006#discussion_r3215386716

Related: #2425 fixes the same ?? in resetField. This issue is separate, since it also affects mount and form.reset().

Happy to open a PR if a fix is wanted on the 1.x line.

  1. Hi! I'd like to work on this issue as my first contribution to TanStack Form. I noticed the related fix in #2425 and understand this issue concerns the remaining isDefaultValue behavior. Would a separate PR addressing this be welcome, or is someone already working on it?

Sign in to join this conversation.