react-final-form-arrays icon indicating copy to clipboard operation
react-final-form-arrays copied to clipboard

Any way to emptyfy the array ?

Open IzaGz opened this issue 4 years ago • 2 comments

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

IzaGz avatar Jul 23 '20 13:07 IzaGz

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

laukaichung avatar Jul 30 '20 06:07 laukaichung

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)

KingWarin avatar May 23 '24 07:05 KingWarin