T
TanStackβ€’2y ago
generous-apricot

`replaceEqualDeep` returns 'b' if 'a' is [undefined]

I created a reproduction in this typescript playground: https://shorturl.at/txGY9 (The link is reliable, I shortened it to avoid hitting the character limit) If I run this code:
const a = [undefined]
const b = [undefined]
const c = replaceEqualDeep(a, b)
console.log(c === a)
const a = [undefined]
const b = [undefined]
const c = replaceEqualDeep(a, b)
console.log(c === a)
it will log "false" The issue is that this fn is used in useMutationState hook, and depending on select implementation, it can cause an infinite render loop, since a [undefined] output will be considered different on every render
TS Playground - An online editor for exploring TypeScript and JavaS...
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
8 Replies
generous-apricot
generous-apricotOPβ€’17mo ago
Help?
fascinating-indigo
fascinating-indigoβ€’17mo ago
I'll look into it you are right, this test case fails:
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 of undefined', () => {
const current = [undefined]
const next = replaceEqualDeep(current, [undefined])

expect(next).toBe(current)
})
would you like to contribute a fix ? @Manuel Schiller this seems like a regression from when the fix from router was ported over. Judging from the commit message, I think the fix was mean to handle objects where some entries are undefined:
now handles objects with the same number of properties and one property being undefined
but I think it broke equality for undefined being in arrays.
sunny-green
sunny-greenβ€’17mo ago
oh man how can a seemingly simple fix cause that many problems
fascinating-indigo
fascinating-indigoβ€’17mo ago
hehe, lack of tests πŸ˜‰ but now we can add a test-case and fix it and then I guess it doesn't come back. We need better differentiation between objects and arrays I guess
sunny-green
sunny-greenβ€’17mo ago
"then I guess it doesn't come back" I already thought so last time πŸ˜„
fascinating-indigo
fascinating-indigoβ€’17mo ago
fix is shipped with v5.34.2. Thanks for reporting @Kalil and thanks for the fix @Manuel Schiller πŸ™
generous-apricot
generous-apricotOPβ€’17mo ago
Nice! Good to know

Did you find this page helpful?