Skip to content

Commit

Permalink
fix: replaceEqualDeep correctly handles arrays that contain undefined
Browse files Browse the repository at this point in the history
fix ported from query: TanStack/query#7376
  • Loading branch information
schiller-manuel committed May 5, 2024
1 parent b69e8f8 commit 63d603b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
5 changes: 2 additions & 3 deletions packages/react-router/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,9 @@ export function replaceEqualDeep<T>(prev: any, _next: T): T {
for (let i = 0; i < nextSize; i++) {
const key = array ? i : nextItems[i]
if (
!array &&
((!array && prevItems.includes(key)) || array) &&
prev[key] === undefined &&
next[key] === undefined &&
prevItems.includes(key)
next[key] === undefined
) {
copy[key] = undefined
equalItems++
Expand Down
14 changes: 14 additions & 0 deletions packages/react-router/tests/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,20 @@ describe('replaceEqualDeep', () => {

expect(current).toBe(next)
})

it('should return the previous value when both values are an array of undefined', () => {
const current = [undefined]
const next = replaceEqualDeep(current, [undefined])

expect(next).toBe(current)
})

it('should return the previous value when both values are an array that contains undefined', () => {
const current = [{ foo: 1 }, undefined]
const next = replaceEqualDeep(current, [{ foo: 1 }, undefined])

expect(next).toBe(current)
})
})

describe('isPlainArray', () => {
Expand Down

0 comments on commit 63d603b

Please sign in to comment.