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:
update() with unchanged props after reset(newValues) keeps values at the reset value, not the original default.
- 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.
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)
📝 Walkthrough
Walkthrough#
Adds a private
_componentDefaultValuesfield toFormApito separately track component-provideddefaultValues. Theupdate()method is modified to detect when component defaults are unchanged and restore runtimedefaultValues(set byreset(newValues)) instead of overwriting them. Two regression tests and a patch changeset are included.Changes#
Fix reset(newValues) overwrite by update()
FormApi:_componentDefaultValuesfield andupdate()logicpackages/form-core/src/FormApi.ts_componentDefaultValues?: TFormDataprivate field initialized in the constructor. Refactorsupdate()to compare old vs. new component defaults viaevaluate, expandshouldUpdateValuesto gate on that equality, and restore runtimedefaultValueswhen component props are unchanged—preventingreset(newValues)state from being overwritten on re-render.packages/form-core/tests/FormApi.spec.tsupdate()call, and one verifying a subsequent barereset()uses the reset baseline rather than the original component defaults..changeset/fix-reset-update-race.md@tanstack/form-coredocumenting the fix.Estimated code review effort#
🎯 2 (Simple) | ⏱️ ~10 minutes
Possibly related PRs#
FormApi.update()default-values code path to bump_arrayVersionfor array fields whenshouldUpdateValuesis true, directly overlapping with this PR's changes toshouldUpdateValuesandoptions.defaultValueshandling.Suggested reviewers#
Poem#
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
#1681by fixing the race condition where reset(newValues) in onSubmit gets overwritten by subsequent update() calls with unchanged component props.✨ Finishing Touches
🧪 Generate unit tests (beta)
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