Skip to content
Source
Sign inSign up
TanStack/form

fix(form-core): prevent reset(newValues) being overwritten by subsequent update() #2215

OpenZelys-DFKH(GitHub profile, opens in a new tab)wants to merge 2 commits intomain fromrefs/pull/2215/headopened

Fixes #1681

Thanks for the detailed repro. It made this much easier to pin down.

Problem

If you call form.reset(newValues) inside onSubmit and your framework re-renders the component immediately after, the reset values get silently overwritten.

reset(newValues) writes the new values into this.options.defaultValues. When the adapter calls update() on re-render with the same props it had before, update() compares the incoming defaultValues against oldOptions.defaultValues (which now holds the reset values rather than the original component defaults), sees a delta that isn't really a change, sets shouldUpdateValues = true, and reverts the form back to the original defaults.

There's a second issue: this.options = options runs on every re-render, replacing the whole options object. After a reset(), the runtime defaultValues is gone. A later no-arg reset() uses the stale component default instead of the post-submit baseline.

Fix

The root problem is that update() has no way to tell "the component's defaultValues prop actually changed" apart from "someone called reset() and now defaultValues looks different." This adds a private _componentDefaultValues field that tracks what the adapter last explicitly provided, separate from the runtime this.options.defaultValues that reset() writes to.

In update():

  • componentDefaultsUnchanged = evaluate(options.defaultValues, prevComponentDefaultValues) is computed before the field is overwritten.
  • shouldUpdateValues now requires !componentDefaultsUnchanged, so an unchanged re-render can't clobber a reset.
  • When component defaults are unchanged and a prior baseline exists, this.options gets a fresh spread object with the runtime defaultValues preserved. The spread avoids mutating the caller-supplied reference, which matters when the adapter passes a memoized frozen object.

The constructor guard (oldOptions.defaultValues !== undefined) keeps the restoration block from firing on the very first update() call, where oldOptions is the empty class default {}. Initial form values come from baseStoreVal before that first call, so the guard is safe.

Tests

Two regression tests in FormApi.spec.ts:

  1. update() with unchanged props after reset(newValues) keeps values at the reset value, not the original default.
  2. A follow-up no-arg reset() uses the post-submit baseline, not the original component default.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed a race condition where calling reset(newValues) during submission could be overwritten by a later update() triggered by component re-renders when defaultValues props were unchanged.
    • Improved reset() consistency after updates: calling reset() without arguments now uses the reset baseline instead of reusing unchanged component-provided defaults.
    1. coderabbitai[bot](GitHub profile, opens in a new tab)commented

      Review Change Stack

      No actionable comments were generated in the recent review. 🎉

      ℹ️ Recent review info
      ⚙️ Run configuration

      Configuration used: defaults

      Review profile: CHILL

      Plan: Pro

      Run ID: 480d78a8-4036-454b-9bb7-2c6c691b1254

      📥 Commits

      Reviewing files that changed from the base of the PR and between 512626e45462c50699a109022359fc7fc309ec3f and 99028bbd3424332327a5931d8e8d5679193a1978.

      📒 Files selected for processing (1)
      • packages/form-core/src/FormApi.ts
      🚧 Files skipped from review as they are similar to previous changes (1)
      • packages/form-core/src/FormApi.ts

      📝 Walkthrough

      Walkthrough

      Adds a private _componentDefaultValues field to FormApi to separately track component-provided defaultValues. The update() method is modified to detect when component defaults are unchanged and restore runtime defaultValues (set by reset(newValues)) instead of overwriting them. Two regression tests and a patch changeset are included.

      Changes

      Fix reset(newValues) overwrite by update()

      Layer / File(s)Summary
      FormApi: _componentDefaultValues field and update() logic
      packages/form-core/src/FormApi.ts
      Adds _componentDefaultValues?: TFormData private field initialized in the constructor. Refactors update() to compare old vs. new component defaults via evaluate, expand shouldUpdateValues to gate on that equality, and restore runtime defaultValues when component props are unchanged—preventing reset(newValues) state from being overwritten on re-render.
      Regression tests
      packages/form-core/tests/FormApi.spec.ts
      Adds two tests: one verifying reset state survives a no-op update() call, and one verifying a subsequent bare reset() uses the reset baseline rather than the original component defaults.
      Changeset
      .changeset/fix-reset-update-race.md
      Patch changeset entry for @tanstack/form-core documenting the fix.

      Estimated code review effort

      🎯 2 (Simple) | ⏱️ ~10 minutes

      • TanStack/form#2190: Modifies the same FormApi.update() default-values code path to bump _arrayVersion for array fields when shouldUpdateValues is true, directly overlapping with this PR's changes to shouldUpdateValues and options.defaultValues handling.

      Suggested reviewers

      • crutchcorn

      Poem

      🐇 A rabbit once clicked "Submit" with glee, then reset the form to new values, you see. But update() swooped in, sneaky and sly, and wiped all those new defaults — oh my! Now _componentDefaultValues stands guard, keeping resets safe in the form's backyard. 🌿

      🚥 Pre-merge checks | ✅ 5
      ✅ Passed checks (5 passed)
      Check nameStatusExplanation
      Title check✅ PassedThe title accurately summarizes the main fix: preventing reset(newValues) from being overwritten by subsequent update() calls during re-renders.
      Description check✅ PassedThe description covers the problem, solution, and testing approach comprehensively, though the release impact checklist items are not explicitly marked.
      Linked Issues check✅ PassedThe PR directly addresses issue #1681 by fixing the race condition where reset(newValues) in onSubmit gets overwritten by subsequent update() calls with unchanged component props.
      Out of Scope Changes check✅ PassedAll changes are directly scoped to fixing the identified race condition: the changeset entry, the FormApi logic fix, and regression tests validating the fix.
      Docstring Coverage✅ PassedNo functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
      ✏️ Tip: You can configure your own custom pre-merge checks in the settings.
      ✨ Finishing Touches
      🧪 Generate unit tests (beta)
      • Create PR with unit tests

      Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

      ❤️ Share
      Comment `@coderabbitai help` to get the list of available commands and usage tips.
    2. Actionable comments posted: 1

      🤖 Prompt for all review comments with AI agents
      Verify each finding against current code. Fix only still-valid issues, skip the
      rest with a brief reason, keep changes minimal, and validate.
      
      Inline comments:
      In `@packages/form-core/src/FormApi.ts`:
      - Around line 1764-1768: The shouldUpdateValues logic in FormApi.ts uses a
      truthiness check for options.defaultValues, which incorrectly skips legitimate
      falsy default values like 0, empty string, false, and null. Replace the
      truthiness check (options.defaultValues &&) with an explicit presence check such
      as checking if the property is not undefined or exists in the options object, so
      that falsy defaults will be properly evaluated and applied when the form is
      untouched.
      🪄 Autofix (Beta)

      Fix all unresolved CodeRabbit comments on this PR:

      • Push a commit to this branch (recommended)
      • Create a new PR with the fixes

      ℹ️ Review info
      ⚙️ Run configuration

      Configuration used: defaults

      Review profile: CHILL

      Plan: Pro

      Run ID: cb898de9-f728-4db9-8d6a-eb38fbdc581e

      📥 Commits

      Reviewing files that changed from the base of the PR and between 6a73479f2c2659550a6c8b456e3e139bf0d2142e and 512626e45462c50699a109022359fc7fc309ec3f.

      📒 Files selected for processing (3)
      • .changeset/fix-reset-update-race.md
      • packages/form-core/src/FormApi.ts
      • packages/form-core/tests/FormApi.spec.ts

Sign in to join this conversation.