Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/fix-array-reset-rerender.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@tanstack/form-core': patch
'@tanstack/react-form': patch
---

Bump `_arrayVersion` when resetting array fields so adapters re-render after `form.reset()` or `form.resetField()` changes array length.

Fixes #2228
13 changes: 13 additions & 0 deletions packages/form-core/src/FormApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1847,6 +1847,15 @@ export class FormApi<
fieldMetaBase,
})
})

const helper = metaHelper(this)
for (const fieldKey of Object.keys(
this.fieldInfo,
) as DeepKeys<TFormData>[]) {
if (Array.isArray(this.getFieldValue(fieldKey))) {
helper.bumpArrayVersion(fieldKey)
}
}
}

/**
Expand Down Expand Up @@ -2954,6 +2963,10 @@ export class FormApi<
: prev.values,
}
})

if (Array.isArray(this.getFieldValue(field))) {
metaHelper(this).bumpArrayVersion(field)
}
Comment on lines +2966 to +2969

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Add test coverage for resetField array version bump.

The reset() path has a dedicated test in FormApi.spec.ts, but the resetField() array-version bump (lines 2967–2968) has no corresponding test. A regression could silently break this path.

🧪 Suggested test
it('should bump array version when resetField is called on an array field', () => {
  const form = new FormApi({
    defaultValues: {
      items: [1, 2, 3],
    },
  })
  form.mount()

  const field = new FieldApi({ form, name: 'items' })
  field.mount()

  form.setFieldValue('items', [1, 2, 3, 4, 5])
  // _arrayVersion is bumped by pushFieldValue or setFieldValue paths

  form.resetField('items')

  expect(form.getFieldValue('items')).toEqual([1, 2, 3])
  expect(form.getFieldMeta('items')?._arrayVersion).toBe(1)
})
🤖 Prompt for 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.

In `@packages/form-core/src/FormApi.ts` around lines 2966 - 2969, Add a
FormApi.spec.ts test covering resetField on an array field, using the existing
FormApi, FieldApi, and resetField test patterns. Set an array value, call
resetField, then assert the default array is restored and the field meta
_arrayVersion is bumped as expected.

}

/**
Expand Down
17 changes: 17 additions & 0 deletions packages/form-core/tests/FormApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,23 @@ describe('form api', () => {
})
})

it('should bump array version when resetting to a shorter array', () => {
const form = new FormApi({
defaultValues: {
items: [1, 2, 3],
},
})
form.mount()

const field = new FieldApi({ form, name: 'items' })
field.mount()

form.reset({ items: [4] })

expect(form.getFieldValue('items')).toEqual([4])
expect(form.getFieldMeta('items')?._arrayVersion).toBe(1)
})

it('form should reset default value when resetting in onSubmit', async () => {
const defaultValues = {
name: '',
Expand Down
42 changes: 42 additions & 0 deletions packages/react-form/tests/useField.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1644,6 +1644,48 @@ describe('useField', () => {
expect(getByTestId('item-0')).toHaveTextContent('Alice')
})

it('should rerender array field when reset to a shorter array', async () => {
// Regression test for https://github.com/TanStack/form/issues/2228
function Comp() {
const form = useForm({
defaultValues: {
items: [1, 2, 3],
},
})

return (
<>
<button
data-testid="reset"
type="button"
onClick={() => form.reset({ items: [4] })}
>
Reset
</button>
<form.Field name="items" mode="array">
{(field) => (
<ol data-testid="list">
{field.state.value.map((item, i) => (
<li key={i} data-testid={`item-${i}`}>
{item}
</li>
))}
</ol>
)}
</form.Field>
</>
)
}

const { getByTestId } = render(<Comp />)
expect(getByTestId('list').children).toHaveLength(3)

await user.click(getByTestId('reset'))

expect(getByTestId('list').children).toHaveLength(1)
expect(getByTestId('item-0')).toHaveTextContent('4')
})

it('should handle defaultValue without setstate-in-render error', async () => {
// Spy on console.error before rendering
const consoleErrorSpy = vi.spyOn(console, 'error')
Expand Down