From 45b83ebbb397a87a5d2893c08de3617870fb3746 Mon Sep 17 00:00:00 2001 From: daishi Date: Wed, 16 Oct 2024 10:02:53 +0900 Subject: [PATCH 1/2] add failing test --- tests/vanilla/shallow.test.tsx | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/vanilla/shallow.test.tsx b/tests/vanilla/shallow.test.tsx index 07f0561ed5..481aa6b109 100644 --- a/tests/vanilla/shallow.test.tsx +++ b/tests/vanilla/shallow.test.tsx @@ -97,6 +97,11 @@ describe('shallow', () => { const b = new URLSearchParams({ zustand: 'shallow' }) expect(shallow(a, b)).toBe(false) }) + + it('should work with nested arrays (#2794)', () => { + const arr = [1, 2] + expect(shallow([arr, 1], [arr, 1])).toBe(true) + }) }) describe('unsupported cases', () => { From a5496d7a6d12a4b11d46bc3ce42c108d8c339c19 Mon Sep 17 00:00:00 2001 From: daishi Date: Wed, 16 Oct 2024 10:06:22 +0900 Subject: [PATCH 2/2] add a fallback --- src/vanilla/shallow.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/vanilla/shallow.ts b/src/vanilla/shallow.ts index 84b1c42d2f..3f35ec50b6 100644 --- a/src/vanilla/shallow.ts +++ b/src/vanilla/shallow.ts @@ -40,10 +40,14 @@ export function shallow(objA: T, objB: T): boolean { nextA.value.length === 2 && nextB.value.length === 2 ) { - return compareMapLike( - objA as Iterable<[unknown, unknown]>, - objB as Iterable<[unknown, unknown]>, - ) + try { + return compareMapLike( + objA as Iterable<[unknown, unknown]>, + objB as Iterable<[unknown, unknown]>, + ) + } catch { + // fallback + } } while (!nextA.done && !nextB.done) { if (!Object.is(nextA.value, nextB.value)) {