Skip to content

Commit d7f657d

Browse files
committed
Update array reference in useArrayControl hook
1 parent e6d3369 commit d7f657d

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

packages/core/src/hooks/useArrayControl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export const useArrayControl = <V>({ name }: ArrayControlConfig<V>): ArrayContro
4141

4242
const [newItems, output] = update(items);
4343

44-
setItems(newItems);
44+
setItems([...newItems]);
4545

4646
if (updateErrors && Array.isArray(errors)) {
4747
setErrors(typeof updateErrors === 'function' ? updateErrors(errors, items) : update(errors)[0]);

packages/core/tests/hooks/useArrayControl.test.tsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,3 +519,21 @@ describe('removeAt', () => {
519519
expect(bag.getFieldTouched(createPxth(['arr']))).toStrictEqual([emptyTouched, emptyTouched]);
520520
});
521521
});
522+
523+
describe('reference updates after changing value', () => {
524+
it('setItem should update array reference', () => {
525+
const [{ result }, bag] = renderArrayControl(createPxth<unknown[]>(['arr']), {
526+
initialValues: {
527+
arr: [1, 2, 3, 4],
528+
},
529+
});
530+
531+
const prevRef = bag.getFieldValue(bag.paths.arr);
532+
533+
act(() => {
534+
result.current.setItem(2, 1);
535+
});
536+
537+
expect(bag.getFieldValue(bag.paths.arr)).not.toBe(prevRef);
538+
});
539+
});

0 commit comments

Comments
 (0)