select memoization not working when select return value contains a Date object re-rendering in the same render cycle #8680
Send to my agentDescribe the bug#
I ran into a case where select memoization is not stable and causes an endless render cycle in my application.
It happens when I reset some state during rendering (this was recommended by dan abramov for saving a dom render cycle compared to useEffect) and returning a Date object as part of the select function return value.
Your minimal, reproducible example#
https://stackblitz.com/edit/tanstack-query-nzs2pqnq?file=src%2Findex.tsx
Steps to reproduce#
- Start the app
- Type
ainto the text field - Remove the
afrom the text field - It crashes in an endless render cycle### Expected behavior
As a user I would expect that the output of the select is memoized even when it creates Date objects, and even when using state setters during rendering.
How often does this bug happen?#
Every time
Screenshots or Videos#
No response
Platform#
- OS: macOS
- Browser: Chrome
- Version: 5.66.8
Tanstack Query adapter#
react-query
TanStack Query version#
5.66.8
TypeScript version#
5.7.2
Additional context#
No response
TkDodo closed this
TkDodo added labelsinvalid
@TkDodo i missed the dependency array in the example but not in my real application. I now could reproduce the issue in the example also with the dependency array. I've updated the example and reproduction. Now it's necessary to type something into the text field and remove it again (like in my real app). Please reopen.
okay I see why this is happening now, but not sure what to do against it. We keep track of the
currentQuery, and this update happens in an effect (as it should). Now when you immediately trigger a re-render, the effect cannot run, so we have never committed that computation fromselect. That’s why when the re-render then starts, it will have to re-compute.So I’m not sure if you can rely on
data !== prevData.currentto initiate the re-render. The whole pattern feels a bit “off” with trackingprevDatain a ref. Reading from refs during render is also discouraged by react.Well this pattern was somewhen suggested by dan abramov to save dom render cycles 🤷
I'd be happy if this limitation was mentioned in the docs: https://tanstack.com/query/latest/docs/framework/react/guides/render-optimizations#memoization
I guess you are referring to this pattern:
https://react.dev/learn/you-might-not-need-an-effect#adjusting-some-state-when-a-prop-changes
it’s a bit different because both values are held in react state, but the state of the query lives outside of react. Those things only work if all involved state lives within react.
Yes. Not sure anymore why we use a ref, using that pattern in our codebase for several years without issues.
I don't think the ref matters much here, it's related to the query store living outside of react state.
Sign in to join this conversation.
your
useCallbackis missing the dependency array. Adding it will make the sandbox work:https://stackblitz.com/edit/tanstack-query-4nrxbwdv?file=src%2Findex.tsx
when select is memoized, it will only re-run when
datafrom thequeryFnor the select function itself changes. In your case, you weren’t memoizing correctly.Additionally, we do structural sharing on the result of
select, which only works for json serializable values. This different optimization kicked in and gave you referentially stable values back even thoughselectwas running on every render. That’s why it seems to work if there is no Date object.