final-form / react-final-form-arrays

A component for rendering and editing arrays 🏁 React Final Form
MIT License
205 stars 70 forks source link

Any way to emptyfy the array ? #144

Open IzaGz opened 4 years ago

IzaGz commented 4 years ago

Request for the feqture! We have shift, pop, remove, but any one can clear the whole array for making it empty?

laukaichung commented 4 years ago

I was facing the similar problem with removing multiple items. Using field.remove() in a loop wouldn't work properly. I just found a method called removeBatch() that is covered in https://final-form.org/docs/final-form-arrays/api.

So I'm using fields.removeBatch(indexArray) to remove multiple items from an array. I wonder if it is the correct way of doing it since it isn't mentioned in the React Final Form Arrays document

KingWarin commented 6 months ago

I know this is old, but while searching for a solution I ran into this issue multiple times, so in case anyone stops by searching for an easy solution...

I first used removeBatch like suggested by @laukaichung. But sometimes this wouldn't clear the whole array for me (might be with my implementation of creating the indexArray...)

Just now I realized there is a way easier solution to emptying the whole array: Just change the fields value to undefined using final-forms change method. So instead of: form.mutators.removeBatch(name, indexArray) just use: form.change(name, undefined)