refine icon indicating copy to clipboard operation
refine copied to clipboard

fix: hydrate field array values after form remount

Open swayamshah01 opened this issue 5 months ago • 3 comments

Description

Fixes #7401.

This PR fixes an issue where useFieldArray.fields could be empty after navigating away from an edit form and then returning to it in an SPA.

The problem occurs when components using useFieldArray are rendered only after useForm's formLoading becomes false. In that flow, the form receives the query data, but the field array may not be registered at the time values are applied. Hydrating fields with individual setValue calls does not reliably initialize React Hook Form's field array internals, so fields can remain empty even though the query response contains array data.

This updates query data hydration to use React Hook Form's reset API with keepDirtyValues: true. That allows field arrays to be initialized from the fetched record while preserving dirty user edits. The reset payload also merges the current form values with the query data so registered fields that are not present in the API response keep their existing/default values.

Changes

  • Replaced initial query-data hydration via registered-field setValue calls with reset.
  • Preserved dirty values during hydration with keepDirtyValues: true.
  • Added a regression test for navigating from an edit form to a list route and back to the edit form.
  • Added a patch changeset for @refinedev/react-hook-form.

Tests

pnpm.cmd --filter @refinedev/react-hook-form test
pnpm.cmd --filter @refinedev/react-hook-form build

swayamshah01 avatar Jun 05 '26 16:06 swayamshah01

🦋 Changeset detected

Latest commit: c76e6db46e26f6b803c99f0ebefb3ca3d5a8ce4c

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

changeset-bot[bot] avatar Jun 05 '26 16:06 changeset-bot[bot]

Hi, there's already a PR open for this issue: https://github.com/refinedev/refine/pull/7402. Do you see any problems with its implementation? If so, let's discuss there instead of creating additional PRs!

broekew avatar Jun 05 '26 17:06 broekew

Thanks for pointing me to #7402.

I checked its implementation and it solves the initial delayed useFieldArray registration case with reset, which is the same direction I took here.

One possible edge case I noticed is that #7402 only calls reset once via hasResetRef, then falls back to setValue for later query-data updates. Since setValue does not reliably update useFieldArray.fields metadata/length, I think field arrays may still get out of sync if the same mounted form receives new query data with a different array length.

For example:

  • initial query data has tags: [{ value: "react" }, { value: "refine" }]
  • later query data for the same mounted form has tags: [{ value: "react" }]
  • after the first reset, the fallback setValue path may update values but leave fields.length as 2

My PR resets on each query data change with keepDirtyValues: true, while merging current form values with query data to avoid dropping fields that are not present in the response.

Happy to move this discussion to #7402 if you prefer, or close this PR if maintainers decide #7402’s approach is sufficient.

swayamshah01 avatar Jun 06 '26 03:06 swayamshah01

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Aug 05 '26 05:08 stale[bot]