T
TanStackβ€’2y ago
rare-sapphire

`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
rare-sapphire
rare-sapphireOPβ€’2y ago
Help?
extended-salmon
extended-salmonβ€’2y 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.
rare-sapphire
rare-sapphireβ€’2y ago
oh man how can a seemingly simple fix cause that many problems
extended-salmon
extended-salmonβ€’2y 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
rare-sapphire
rare-sapphireβ€’2y ago
"then I guess it doesn't come back" I already thought so last time πŸ˜„
extended-salmon
extended-salmonβ€’2y ago
fix is shipped with v5.34.2. Thanks for reporting @Kalil and thanks for the fix @Manuel Schiller πŸ™
rare-sapphire
rare-sapphireOPβ€’2y ago
Nice! Good to know

Did you find this page helpful?