react-hook-form icon indicating copy to clipboard operation
react-hook-form copied to clipboard

issue: useFieldArray cut off null values from array and cause broken indexing next entities

Open dzwiedziu-nkg opened this issue 1 year ago • 1 comments

Version Number

7.52.1

Codesandbox/Expo snack

https://codesandbox.io/s/react-hook-form-usefieldarray-nested-arrays-forked-6jn6cm?file=/src/index.js

Steps to reproduce

I add null values to array in defaultValues. useFieldArray cut off null values from array. So indexing of is invalid. We get only two array elements with broken index.

Its happen because you wrap by compact function in this: https://github.com/react-hook-form/react-hook-form/blob/ee7b5883d029e084e433fdd873cac21dfff4a11e/src/logic/createFormControl.ts#L539

Expected behaviour

When you change react-hook-form version to 7.12.2 in packages.json this example works good. We get all array elements with correct index.

What browsers are you seeing the problem on?

Chrome

Relevant log output

No response

Code of Conduct

  • [X] I agree to follow this project's Code of Conduct

dzwiedziu-nkg avatar Jul 04 '24 18:07 dzwiedziu-nkg

I agree if it's marked as a duplicate of #12053. The cause of the error is the same. The example is different because I am using complex nested fields instead of an array of strings.

dzwiedziu-nkg avatar Jul 05 '24 06:07 dzwiedziu-nkg

would you like to send a PR with this?

bluebill1049 avatar Aug 31 '24 03:08 bluebill1049

I can create PR if no one else is working on this

MathurAditya724 avatar Sep 16 '24 04:09 MathurAditya724

I can create PR if no one else is working on this

Yes, please would be great to attach some tests with it as well.

bluebill1049 avatar Sep 21 '24 05:09 bluebill1049

but why we have null in the default values? field array doesn't support flat array.

Does not support flat field array.

https://react-hook-form.com/docs/usefieldarray

bluebill1049 avatar Oct 12 '24 05:10 bluebill1049

field array doesn't support flat array.

Why?

ErBlack avatar Oct 14 '24 15:10 ErBlack