AlexWayne
AlexWayne
TtRPC
Created by AlexWayne on 6/14/2024 in #❓-help
v10 useQueries does not return a stable reference
Ended up with
function useMemoizedResultData<T>(results: { data: T }[]): T[] {
const previousResultData = useRef<T[]>([])
const resultData = results.map((result) => result.data)

if (areArraysEqual(previousResultData.current, resultData)) {
return previousResultData.current
}

previousResultData.current = resultData
return resultData
}

/** Returns true when `a` and `b` are arrays with perfectly identical contents. */
function areArraysEqual(a?: unknown[], b?: unknown[]): boolean {
if (a === b) return true
if (a?.length !== b?.length) return false
return !!a?.every((item, i) => item === b?.[i])
}
function useMemoizedResultData<T>(results: { data: T }[]): T[] {
const previousResultData = useRef<T[]>([])
const resultData = results.map((result) => result.data)

if (areArraysEqual(previousResultData.current, resultData)) {
return previousResultData.current
}

previousResultData.current = resultData
return resultData
}

/** Returns true when `a` and `b` are arrays with perfectly identical contents. */
function areArraysEqual(a?: unknown[], b?: unknown[]): boolean {
if (a === b) return true
if (a?.length !== b?.length) return false
return !!a?.every((item, i) => item === b?.[i])
}
Which feels like a bit of a hack, but it seems to work.
5 replies
TtRPC
Created by AlexWayne on 6/14/2024 in #❓-help
v10 useQueries does not return a stable reference
I guess trpc v11 and tanstack query v5 fix this with the combine option. Maybe I should focus on being able to upgrade instead
5 replies
TtRPC
Created by AlexWayne on 6/14/2024 in #❓-help
v10 useQueries does not return a stable reference
This does work, which means the data prop is stable
const foo = useMemo(() => {
console.log('memo recreated')
return results.map((result) => result.data?.id)
}, [results[0]?.data])
const foo = useMemo(() => {
console.log('memo recreated')
return results.map((result) => result.data?.id)
}, [results[0]?.data])
but I'm not seeing how to get a stable array of the data objects themselves
5 replies