Skip to content
This repository was archived by the owner on Jul 27, 2022. It is now read-only.

Commit 4f9ae54

Browse files
committed
Fix(useFieldArray): reset fields not working properly
1 parent b13cc18 commit 4f9ae54

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

.changeset/soft-lamps-cry.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-cool-form": patch
3+
---
4+
5+
Fix(useFieldArray): reset fields not working properly

src/useFieldArray.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,14 @@ export default <T = any, V extends FormValues = FormValues>(
8080
() => ({
8181
reset: () =>
8282
setFields((prevFields) => {
83-
const fieldLength = getState(`values.${name}`)?.length || 0;
83+
const currFields = getState(`values.${name}`);
84+
const currFieldsLength = currFields?.length || 0;
8485

85-
if (prevFields.length === fieldLength) return prevFields;
86+
if (prevFields.length === currFieldsLength) return prevFields;
87+
if (prevFields.length < currFieldsLength) return currFields;
8688

8789
const nextFields = [...prevFields];
88-
nextFields.length = fieldLength;
90+
nextFields.length = currFieldsLength;
8991

9092
return nextFields;
9193
}),

0 commit comments

Comments
 (0)