Skip to content

fix(react-hook-form): reset form state on query data change to fix useFieldArray on revisit#7452

Open
devavijha wants to merge 1 commit into
refinedev:mainfrom
devavijha:fix/use-field-array-empty-on-revisit
Open

fix(react-hook-form): reset form state on query data change to fix useFieldArray on revisit#7452
devavijha wants to merge 1 commit into
refinedev:mainfrom
devavijha:fix/use-field-array-empty-on-revisit

Conversation

@devavijha

Copy link
Copy Markdown

Problem

When navigating away from an edit form and returning to it within a SPA, useFieldArray.fields is empty on every visit after the first. The issue is in packages/react-hook-form/src/useForm/index.ts.

The existing approach calls applyValuesToFields(getRegisteredFields(), data, false) which uses setValue on individual field paths and tracks synced paths in syncedFieldsRef. On re-visit:

  • syncedFieldsRef accumulates paths across mount/unmount cycles
  • Fields registered by useFieldArray are not present in the set when the effect runs again on the second visit
  • The guard incorrectly skips them, so useFieldArray.fields stays empty

Minimal reproduction is in the issue: https://codesandbox.io/p/sandbox/t6pxft

Fix

Replace applyValuesToFields(getRegisteredFields(), data, false) with reset(data, { keepDirtyValues: true }).

The reset API from react-hook-form re-initialises the entire form state, including all field arrays, on each new query result. The keepDirtyValues option ensures that any in-progress user edits are not discarded.

Changes:

  • Added reset to the destructured result from useHookForm
  • Replaced applyValuesToFields call inside applyQueryValues with reset(data, { keepDirtyValues: true })

Closes #7401

…eFieldArray on revisit

When navigating back to an edit form in a SPA, useFieldArray.fields was empty
on every visit after the first. The field-by-field setValue approach relied on
syncedFieldsRef which accumulated paths across navigations, causing the guard
to skip re-populating fields registered by useFieldArray on re-mount.

Replace applyValuesToFields with reset(data, { keepDirtyValues: true }). This
re-initialises the full form state including field arrays on each new query
result. The keepDirtyValues option preserves user edits that are already in
progress.

Fixes refinedev#7401
@changeset-bot

changeset-bot Bot commented Jun 12, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 8415faa

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@refinedev/react-hook-form Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this 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.

[BUG] useFieldArray fields empty on second visit when using useForm

1 participant