5

I'm facing an issue where the fields from useFieldArray (react-form-hook) don't get updated when they are replaced from a useEffect hook.

Here's a minimal example that demonstrates it. MyComponent creates a field array using a control provided from its parent component. The field array doesn't get updated and the items don't get rendered the first time (it does in subsequent renders)

const MyComponent = ({ control, items }) => {
  const { fields, append, remove, replace, update } = useFieldArray({
    control,
    name: "items",
  });


  useEffect(() => {
    replace(items)
  }, [items]);

  return (
    <>
      {fields.map((item, index) => (
        <p key={item.id}>{item.name}</p>
      )}
    </>)

This component is inside a parent

 const [items, setItems] = useState([])
 const { handleSubmit, control, setValue, getValues, reset, watch } = useForm({
    defaultValues
  });

 useEffect(() => {

 ... 
 // items is a list of {id: '', name: ''} objects
 setItems(items); 
 }, [])

 ...
 
 return (
<form onSubmit={handleSubmit(saveData)}>
   <Controller ... > <TextField ... /> </Controller>
   <MyComponent items={items} control={control} getValues={getValues}/>
</>
)

items always has items (not empty).

What I think happening is that fields doesn't get updated at the same time when replace(items) is done and due to some (timing?) issue it is always empty.

Any idea what could be happening and how to avoid it?

Sample code: https://codesandbox.io/s/react-hook-form-usefieldarray-template-forked-2ll8zb?file=/src/index.js:0-1013

The UI basically creates a field array using useFieldArray which receives the Controller from a parent component. The field array is rendered and the purpose is to add/remove items dynamically and reflect the changes on screen. Add/remove functionality is not implemented -- the issue occurs in the first render.

5
  • "items always has items (not empty)" - items is an empty array on the initial render. Also, setItems(items); seems a bit useless unless this is a different items reference. Think you could edit the post to include a more complete minimal reproducible example so we can see what defaultValues is and anything else that wasn't included enough that someone could reproduce this on their end? Commented May 5, 2023 at 5:00
  • Thanks @DrewReese sample code here codesandbox.io/s/… Commented May 5, 2023 at 6:55
  • Perhaps my unfamiliarity with react-hook-form and this useFieldArray hook, but in your sandbox even when doing a double-append (append the same data twice), and/or looping the items array twice... the fields array still only has length 1 and has the last item added. I did the same bit with a third items array element added, and only the final items element is appended. I don't think this is a React hooks or component lifecycle issue. If I do a force rerender, again only the final element is appended, but now it's a duplicate entry. Commented May 5, 2023 at 7:32
  • Perhaps you can explain the UI and what you are expecting the code to do? Is there a reason the form data can't be initialized from the start? Commented May 5, 2023 at 7:33
  • Added some description on what I'm trying to do. This is how it's being used as per the docs codesandbox.io/s/react-hook-form-usefieldarray-ssugn. In my case, I'm using the useFieldArray in a child component with the controller being passed from the parent Commented May 5, 2023 at 8:46

1 Answer 1

1

I upgraded react-hook-form to the latest version (v7.46.1), and used replace(items) instead of append, and I think it produced the result you were expecting.

Here's a fork of your sample code: https://codesandbox.io/s/react-hook-form-usefieldarray-template-forked-nlr4wg?file=/src/index.js

Sign up to request clarification or add additional context in comments.

1 Comment

seems the codesandbox is gone, can you update the codesandbox?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.