Skip to content

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

Open

Description

@maricastroc

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:

const isDefaultValue = evaluate(
curFieldVal,
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
this.getFieldInfo(fieldName)?.instance?.options.defaultValue ??
getBy(this.options.defaultValues, fieldName),
)

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: #2006 (comment)

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.

Activity

  1. fenet-as commented on Oct 12, 2026

    @fenet-as

    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 up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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