Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ArrayInput breaks when value is null #7754

Closed
mccasey opened this issue May 24, 2022 · 8 comments · Fixed by #8449
Closed

ArrayInput breaks when value is null #7754

mccasey opened this issue May 24, 2022 · 8 comments · Fixed by #8449

Comments

@mccasey
Copy link

mccasey commented May 24, 2022

I'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):
@slax57
Copy link
Contributor

slax57 commented May 25, 2022

Hi, and thank you for submitting this.
This is a limitation of react-hook-form's useFieldArray.
Indeed if you take a look at this code sandbox, I have the same problem using their hook directly (and this hook is used internally by react-admin):
https://codesandbox.io/s/react-hook-form-get-started-forked-zp80wb?file=/src/index.js

Maybe we could detect this on our side and replace the value with an empty array, but I'm not really sure about the impacts this would have.

Can you tell us more about your use case?

Thanks

@mccasey
Copy link
Author

mccasey commented May 26, 2022

Use case being that some field values are null. This is only an issue with rendering array inputs, all other inputs appear to render fine when a field value is null. Since react-hook-form doesn't like null values for arrays the only work around for this right now is to remove all null values and replace them with undefined when the data is returned from the api.

@slax57
Copy link
Contributor

slax57 commented May 30, 2022

Okay so actually, this problem seems to have been solved as of react-hook-form v7.29.0 (although I can't find evidence in the changelog).
We currently are on version 7.27.0, so maybe a simple version upgrade could fix this (and if not, then the issue may be on our side).
Will label this as a bug since we now have a way to fix this.

@slax57
Copy link
Contributor

slax57 commented May 31, 2022

Fixed by #7768

@slax57 slax57 closed this as completed May 31, 2022
@julienV
Copy link
Contributor

julienV commented Jul 23, 2022

Hi, i still can see this issue on react admin 4.2.4...

EDIT: workaround for me is to filter out the response in the dataprovider...

@djhi djhi reopened this Jul 27, 2022
@djhi
Copy link
Collaborator

djhi commented Jul 27, 2022

Indeed and it seems that it is expected in react-hook-form: react-hook-form/react-hook-form#4826

@slax57
Copy link
Contributor

slax57 commented Aug 8, 2022

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 react-hook-form/react-hook-form#4826 (comment) .

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 an ArrayInput.

JinParc pushed a commit to JinParc/react-admin that referenced this issue Oct 25, 2022
@WiXSL
Copy link
Contributor

WiXSL commented Nov 30, 2022

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 react-hook-form/react-hook-form#4826 (comment) .

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 an ArrayInput.

@slax57
Your proposed change was added here: react-hook-form/react-hook-form#9170
and released in version 7.38 of react-hook-form

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants