refine icon indicating copy to clipboard operation
refine copied to clipboard

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

Open broekew opened this issue 7 months ago • 5 comments

Describe the bug

useFieldArray.fields is empty when navigating back to an edit form that uses useForm in a SPA. On the first visit the fields populate correctly, but on every subsequent visit they do not.

Potential fix:

- applyValuesToFields(getRegisteredFields(), data, false);
+ reset(data, { keepDirtyValues: true });

Here: https://github.com/refinedev/refine/blob/main/packages/react-hook-form/src/useForm/index.ts#L271

Steps To Reproduce

Minimal reproducible example on codesandbox here: https://codesandbox.io/p/sandbox/t6pxft.

  1. Go to the edit page -> useFieldArray.fields is populated properly
  2. Go back to the list page
  3. Go back to the edit page -> no fields returned by useFieldArray
  4. Repeat steps 2 and 3 notice how it is consistently reproducible.
  5. Perform a full page refresh -> form works again (one time)

Expected behavior

fields should reflect the array returned by getOne

Packages

  • @refinedev/core: 5.0.12
  • @refinedev/react-hook-form: 5.0.4

Additional Context

The issue seems to be specifically when the useFieldArray is guarded behind the useForm's formLoading value, indicating a possible race condition.

Workaround

Do not guard components containing useFieldArray by formLoading. Prevent guarding at all or guard with query.data, e.g.:

- {formLoading && <ComponentContainingUseFieldArray />}
+ {query?.data && <ComponentContainingUseFieldArray />}

Additional comments

Update 29-04: the same root issue seems to cause an issue when useFieldArray is used with useForm on a MUI Grid. See https://codesandbox.io/p/sandbox/refine-fieldarray-datagrid-2-76m996. The currently open PR appears to fix both (at least in our environment).

broekew avatar Apr 20 '26 14:04 broekew

I can reproduce this on my end. The issue is that applyValuesToFields() misses useFieldArray fields because they register after the microtask runs. Using reset() instead handles field arrays natively through RHF. Working on a PR.

zons-zhaozhy avatar May 11 '26 12:05 zons-zhaozhy

I am looking into this

OfficialRohitSharma442 avatar Jun 05 '26 06:06 OfficialRohitSharma442

Hi, I’d like to take this up.

I’ll investigate the useFieldArray empty-on-second-visit behavior in @refinedev/react-hook-form, reproduce it with a focused test case, and open a PR with a fix once I’ve isolated the root cause.

swayamshah01 avatar Jun 05 '26 16:06 swayamshah01

Can i try ??

Nexus334023K avatar Jun 12 '26 09:06 Nexus334023K

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 12 '26 01:08 stale[bot]