Skip to content

Conversation

kusiewicz
Copy link
Contributor

@kusiewicz kusiewicz commented Oct 14, 2025

🎯 Changes

Fixes: #1808

If this idea make sense, I will add tests

After calling form.deleteField('fieldName'), the field is removed from form.state.values, but after a moment it returns with defaultValue and appears at the end of the values object.

The problem occurred due to React lifecycle and the logic in FieldApi.mount()

Sequence of events:

  1. User clicks delete -> form.deleteField('field') executes -> value is remo ved from form.state.values -> field metadata is removed - cool, but
  2. React detects state change -> component re-renders
  3. During re-render the field is still in the DOM (not unmounted)
  4. React calls FieldApi.update() or lifecycle hooks -> this calls setFieldValue
  5. In FieldApi.mount() there was logic:
   if ((this.options.defaultValue as unknown) !== undefined) {
     this.form.setFieldValue(this.name, this.options.defaultValue as never, {
       dontUpdateMeta: true,
     })
   }

I think this uncoditionally restored defaultValue on every mount.update

PS: Even if we add a check in mount() - setFieldValue can be called from other places (e.g., from FieldApi.update() during React lifecycle).

I added a mechanism for tracking intentionally deleted fields to prevent their their accidental restoration.

  1. Added _deletedFields: Set<string> in FormApi - it stores names of fields that were intentionally deleted by the user
  2. deleteField() adds the field to _deletedFields
  3. setFieldValue() blocks operations on deleted fields

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested this code locally with pnpm test:pr.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

Copy link

changeset-bot bot commented Oct 14, 2025

⚠️ No Changeset found

Latest commit: d0d2271

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

nx-cloud bot commented Oct 14, 2025

🤖 Nx Cloud AI Fix Eligible

An automatically generated fix could have helped fix failing tasks for this run, but Self-healing CI is disabled for this workspace. Visit workspace settings to enable it and get automatic fixes in future runs.

To disable these notifications, a workspace admin can disable them in workspace settings.


View your CI Pipeline Execution ↗ for commit d0d2271

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ❌ Failed 1m 55s View ↗
nx run-many --target=build --exclude=examples/** ✅ Succeeded 24s View ↗

☁️ Nx Cloud last updated this comment at 2025-10-14 18:17:41 UTC

Copy link

pkg-pr-new bot commented Oct 14, 2025

More templates

@tanstack/angular-form

npm i https://pkg.pr.new/@tanstack/angular-form@1809

@tanstack/form-core

npm i https://pkg.pr.new/@tanstack/form-core@1809

@tanstack/form-devtools

npm i https://pkg.pr.new/@tanstack/form-devtools@1809

@tanstack/lit-form

npm i https://pkg.pr.new/@tanstack/lit-form@1809

@tanstack/react-form

npm i https://pkg.pr.new/@tanstack/react-form@1809

@tanstack/react-form-devtools

npm i https://pkg.pr.new/@tanstack/react-form-devtools@1809

@tanstack/solid-form

npm i https://pkg.pr.new/@tanstack/solid-form@1809

@tanstack/svelte-form

npm i https://pkg.pr.new/@tanstack/svelte-form@1809

@tanstack/vue-form

npm i https://pkg.pr.new/@tanstack/vue-form@1809

commit: d0d2271

@LeCarbonator
Copy link
Contributor

Blocked by #1729 . This seems critical to test with the linked PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Deleted fields with defaultValue are restored on re-render after deleteField()

2 participants