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

Object key gets removed after removing last item in field array #95

Open
erickjth opened this issue Apr 3, 2023 · 2 comments
Open

Object key gets removed after removing last item in field array #95

erickjth opened this issue Apr 3, 2023 · 2 comments

Comments

@erickjth
Copy link

erickjth commented Apr 3, 2023

Are you submitting a bug report or a feature request?

bug report

What is the current behavior?

Currently, when adding an item into an array key, and then removing the last one, the key gets removed from the original object, no matter if the key was initialized as an empty array.

What is the expected behavior?

Keep the key in the form values, as an empty array.

Sandbox Link

https://codesandbox.io/s/react-final-form-field-arrays-forked-xopb2v

What's your environment?

"final-form": "4.20.9",
"final-form-arrays": "3.1.0",
"react": "17.0.0",
"react-dom": "17.0.0",
"react-final-form": "6.5.9",
"react-final-form-arrays": "3.1.3",
@sudkumar
Copy link

sudkumar commented Apr 7, 2023

Hi @erickjth

I was facing the same issue with recent update of 3.1.0. With this update, the behaviour of remove and removeBatch has been changed. Checkout the release notes for more info.

Temp Fix

Just revert final-form-arrays back to 3.0.2 and all good.

Long term solution

For a long terms solution, I think it is better to always consider form fields as optional. There might be other cases where the code might not work if we don't handle it. I am migrating my codebases to handle these cases. Also worth noting that a field's values (useField) is returned as empty string ('') when it's not defined, even for arrays. So this should be handled as well. In summary

  • Any field in the form can be undefined
  • Always check for field type (array/string/object) before doing any manipulation or operations

@peruukki
Copy link

peruukki commented Apr 19, 2023

Having to deal with optionality adds unnecessary complexity to me. Previously (with 3.0.2), we could initialize the value to [] and only deal with arrays, now (3.1.0) we have to sprinkle ?./?? etc. all around. I guess now it would be better to initialize to undefined instead, to notice possible problems with it more easily.

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

No branches or pull requests

3 participants