Skip to content
Source
Sign inSign up
TanStack/query

select memoization not working when select return value contains a Date object re-rendering in the same render cycle #8680

Send to my agent
Closedfabb(GitHub profile, opens in a new tab) opened this issue 7 comments

Describe 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

  1. Start the app
  2. Type a into the text field
  3. Remove the a from the text field
  4. 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

  1. your useCallback is 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 data from the queryFn or 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 though select was running on every render. That’s why it seems to work if there is no Date object.

  2. TkDodo closed this

  3. TkDodo added labelsinvalid

  4. @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.

  5. 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 from select. 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.current to initiate the re-render. The whole pattern feels a bit “off” with tracking prevData in a ref. Reading from refs during render is also discouraged by react.

  6. 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

  7. 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.

  8. Yes. Not sure anymore why we use a ref, using that pattern in our codebase for several years without issues.

  9. 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.