useQueries and memoization
This might be a lack of react knowledge but I will just go ahead and ask it here. I have a custom hook using useQueries to call multiple endpoints in parallel. The result is mapped using the combine property, outputting a data variable which is used in a component. The component is using useState for several reasons, updating that state triggers the hook once again returning another reference to an array containing the same data. Is their a way to prevent this from happening? In my case I am passing that array to a child component (using memo) but as it's another reference, this component keeps re-rerendering.
12 Replies
genetic-orange•11mo ago
TanStack | High Quality Open-Source Software for Web Developers
Headless, type-safe, powerful utilities for complex workflows like Data Management, Data Visualization, Charts, Tables, and UI Components.

genetic-orangeOP•11mo ago
Can I also use this in the case of useQueries?
genetic-orangeOP•11mo ago
TanStack | High Quality Open-Source Software for Web Developers
Headless, type-safe, powerful utilities for complex workflows like Data Management, Data Visualization, Charts, Tables, and UI Components.

genetic-orangeOP•11mo ago
seems like I can also wrap combine in a useCallback
genetic-orange•11mo ago
Correct
genetic-orangeOP•11mo ago
not sure if I should any dependencies in the useCallback, my eslint is not complaining atm :p
genetic-orange•11mo ago
Not unless you need a variable outside of what's provided from combine
genetic-orangeOP•11mo ago
Nope, I don't, awesome!
Thanks for thinking along, memo now works in the child component as the reference is the same 😉
like-gold•11mo ago
When does the select function itself changed referentially? When this is happening? is code like this useTodos((data) => data.length) referentially unstable?
genetic-orange•11mo ago
Your example is a little minimal. The inline function is not stable, no, but you're referencing a
useTodosCount
example which mentions a component using that custom hook won't render unless the length changesgenetic-orange•11mo ago
This explains it better than I ever could: https://tkdodo.eu/blog/react-query-render-optimizations
React Query Render Optimizations
An advanced guide to minimize component re-renderings when using React Query
like-gold•11mo ago
@troywoy
I have this custom hook, and returns bigger object, I'm applying select function as optional prop, so using code like this in some component, can we say the prop select is referentially stable, and React won't re-render, because select is applied to the function? Do I need to extract this inline functions above the component to be referentially stable or not?