feat(form-core): [v2] expose the previous value to change listeners #2395
alpha fromrefs/pull/2395/head·opened 🎯 Changes#
Closes #2301.
FormListenerContext and FieldListenerContext now carry an optional prevValue holding the value from immediately before the change that invoked the listener, so a change listener can diff against the value it replaced without keeping its own copy.
prevValueis only set for'change'events; it isundefinedfor'blur','submit','mount','reset'and'unmount'.- Form listeners get the previous form values, field listeners get the previous value in their own scope.
setFieldValuesnapshots_atoms.valuesbefore writing and threads that snapshot through_notifyFieldChange→_notifyFormListener/_notifyEvent→_notifyListener.
Each field scope derives its own previous value with getBy(prevFormValues, field.name) rather than receiving the changed field's value. That is what makes the value correct for ancestors and for watchFields listeners: they see what their scope looked like before the change, not the value of the field that triggered it. It also keeps this to one snapshot per mutation, read from an already immutable atom.
Debounced listeners behave the way the issue describes: the pipeline builds a context per change and LiteDebouncer executes the last one, so a listener that fires after five rapid changes sees the fourth value as prevValue and the fifth as value.
filterFieldValues has a branch that notifies a change even when the filtered array is identical; it now passes the current values so prevValue stays defined for every 'change' event.
✅ Checklist#
- I have followed the steps in the Contributing guide.
- I have tested this code locally with
pnpm test:pr.
I could not run
pnpm test:prlocally in this environment, so I have left that box unchecked rather than claim it. The tests below were written against the existing specs and I am relying on CI here — happy to iterate on anything it flags.
🚀 Release Impact#
- This change affects published code, and I have generated a changeset.
- This change is docs/CI/dev-only (no release).
Tests#
Added to packages/form-core/tests/FormApi/listeners.spec.ts and packages/form-core/tests/FieldApi/listeners.spec.ts:
- the previous value across two consecutive changes, at form and at field level
prevValueisundefinedon a non-change trigger- a debounced listener receives the value from before the change that invokes it
- an ancestor field receives its own previous object value when a descendant changes
Existing change-listener assertions were updated with the new property; the watched-field cases now assert the listening field's own previous value.
Compatibility#
prevValue is a new optional property on the listener context, so existing listeners and their destructuring are unaffected. No other behaviour changes.
Summary by CodeRabbit#
- New Features
- Form and field change listeners now expose
prevValue, the value immediately before the change that triggered the listener. - Previous values are available to regular, linked, ancestor, and debounced listeners; for debounced listeners, this is the value before the change that invokes the listener.
- For non-change events,
prevValueisundefined.
- Form and field change listeners now expose
- 1
Ran
vitestinpackages/form-coreon this branch: one existing spec,runs a linked listener when any of multiple watched fields changes, still asserted the listener context withoutprevValueand failed. fc3181c addsprevValue: ''to both of its assertions. The target field does not change, so its listener receives its own previous value, the empty string.Now run on this branch: form-core 29 files / 606 tests pass, and
tscand eslint are clean. With form-core built, the react, preact, vue, solid and lit adapter suites pass as well. The angular suite errored locally withCannot set base providers because it has already been called, which looks like a TestBed setup problem in my environment rather than this change.galshir(GitHub profile, opens in a new tab) linked this to fix #2301
Sign in to join this conversation.
Navigate logical layers of code changes, visualize relationships, and explore their blast radius.
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
Configuration used: Repository: TanStack/form/.coderabbit.yaml
Review profile: CHILL
Plan: Advanced
Run ID:
94d8697d-1847-4fb9-8ed9-fe7daeb3c58f📥 Commits
Reviewing files that changed from the base of the PR and between a480960979ff080ed2c33ec1047c90fbe90ac53c and fc3181ccf0e39d544798ec6dd7a0301e00dc125d.
📒 Files selected for processing (1)
packages/form-core/tests/FieldApi/listeners.spec.tsIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
📝 Walkthrough
Walkthrough#
Form and field listener contexts now expose optional
prevValuevalues. Change notifications capture and pass the prior form values through listener pipelines. Tests cover change, non-change, ancestor, linked, watcher, and debounced listeners.Changes#
Previous listener values
packages/form-core/src/listeners.*prevValuefields. Listener pipelines pass these values into listener contexts.packages/form-core/src/FormApi/*,packages/form-core/src/FieldApi/*packages/form-core/tests/*/listeners.spec.ts,.changeset/*Priority: ⬇️ Low
Estimated code review effort: 3 (Moderate) | ~20 minutes
Change: Feature
Sequence Diagram(s)#
Suggested reviewers:
lecarbonatorMerge Risk: ⚪ Minimal · up to
fc318Listeners receive the value immediately before a change, including in the array-filter notification path. No material regression is established, so the change is mergeable subject to normal CI.
Architecture Summary#
Architecture risk: 🔵 Low · up to
fc318The change affects 1 system.
Changed systems:
packages/form-coreArchitecture concerns No architecture-level concerns identified.
Review details
Systems and components
Before / after behavior
_notifyEventnow accepts optional previous form values and passes them through each ancestor’s listener notification._notifyListenernow accepts previous form values and supplies each listener pipeline withprevValueresolved from the snapshot by field name, orundefinedwhen no snapshot is provided.setFieldValuenow captures the complete form-value state before applying the field update.🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)#
✅ Passed checks (4 passed)
#2301] requires optionalprevValueon change listener contexts, omission for non-change events, and the value immediately before the change that invokes a debounced listener. The public conte…#2301] by capturing and propagating previous values. The test changes verify the required listener behavior. The changeset documents the public behavior for `@tanstack/form…✨ 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