ArrayInput breaks when value is null
See original GitHub issueI’m not sure this is a bug or if this was intended with 4.x version of react-admin.
What you were expecting: ArrayInputs render when field value is null.
What happened instead:
A cannot read properties of null (reading 'map')
error is thrown here in react-hook-form: https://github.com/react-hook-form/react-hook-form/blob/v7.27.0/src/useFieldArray.ts#L103. It appears unless a field value is undefined react-hook-form will not default to an empty array. Based on line 101 unless a field value is undefined react-hook-form will not default to an empty array.
Steps to reproduce:
Set any field value in the v4 sandbox test data.tsx file intended to be rendered with ArrayInput to null
(ie: ‘backlinks’).
You can see the error here in this sandbox when you navigate to the edit view for post with id 1. https://codesandbox.io/s/boring-benz-5odpst
Environment
- React-admin version: 4.1.0
- React version: 1.17.0
- Browser: Chrome 101.0.4951.64 (Official Build) (x86_64)
- Stack trace (in case of a JS error):
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:8 (6 by maintainers)
According to me this should be fixed on react-hook-form’s side (the ‘map’ error actually comes from their code). That’s what I tried to argue here https://github.com/react-hook-form/react-hook-form/issues/4826#issuecomment-1208145917 .
I don’t see how we could fix this in RA, since we do not know upfront if a given field will be used inside an
ArrayInput
or not.Best we could do is mention in the docs that the value cannot be
null
if it is to be used with anArrayInput
.Fixed by https://github.com/marmelab/react-admin/pull/7768