Skip to content
Source
Sign inSign up
TanStack/query

useMemo does not seem to work in a component doing a query in suspense mode #1250

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

Hi!

I have a query which uses the current timestamp as a variable, and my variables get baked into the query key. I use useMemo to get the timestamp at first render so that it doesn't fire a query at each second. However it seems that useMemo get ignored when in a component doing a query in suspense mode. This causes the query to fetch new data each second and the component never ends loading. I have created a sandbox with minimal changes from your suspense sandbox. It's a bit of a useless example but at least it's familiar and you get it.

https://codesandbox.io/s/usememo-in-suspense-issue-5qb8t?file=/src/components/Projects.js

See line 20-21 of Projects.js, when the query key is memoized in the Projects component the loading never ends. However if I memoize the value in index.js it works as expected.

Since I have a workaround, I don't need a fix per se, but I thought it might be worth reporting. Also it could be linked to #1160 but it's a bit beyond me to be certain. Hope this helps and happy to provide more details / testing if it can help.

Cheers

  1. I'm not sure this has to do with RQ totally, but I could be wrong, but when components in suspense suspend, they are never mounted and their state is not guaranteed to be persisted (as least as far as I know). So when it renders again, the memo is called again as fresh. But I think think we should get a second opinion on this.

  2. tannerlinsley added labelsconcurrent-mode

  3. tannerlinsley added labelsneeds-info

  4. tannerlinsley added labelssuspense

  5. Oh yes. I thought of that and forgot to mention in the post. Thanks for highlighting it.

    I wanted to make a test but to be honest I have no idea how to create a suspending component from scratch 😕. Any idea who could help? I went through the react docs in search for elements but in vain.

  6. I think it is not related to RQ.

    a function like () => `projects ${new Date().toLocaleString()} is not a 'pure function'. It always returns a different value when you call it. So you should not use it with useMemo. useMemo should only be used with a pure function.

    And in suspense mode key is recreated every render. useQuery fetching -> Suspense unmount children -> useQuery fetch done -> Suspense remount children -> recreate params object -> key(date) change, useQuery fetching -> Suspense unmount children -> ....

  7. Thanks for the details @promer94. For the "useMemo should only be used with a pure function", do you have a link that explains that a little bit more?

  8. Sorry for my unclear description.

    when using the useMemo hook, you have to make sure that the function which is used by useMemo always returns same value if dependencies is the same.

    function App() {
      const queryKeyMemoizedInSuspense = useMemo(
        () => `projects ${new Date().toLocaleString()}`,
        []
      );
    }

    In this case, every time App rerenders, the function actually should read current time and return a different result. but useMemo blocks this procedure. This is not correct way of doing memorization

  9. My bad, it was my question that wasn't clear. I understand what a pure functions is, however I did not know that useMemo expected a pure function. I know the memoized function should be free of side effects, but not that it should be pure. I've checked again the docs but there is no mention of that. So I was wondering if you had a link detailing why it needs to be pure.

    From what I understand so far, as long as your dependencies do not change ([] for me), the value will simply be returned without being computed again. I agree that a pure function would feel more logical, but I do not know that it is a requirement.

  10. I'm positive this is a question more about hooks and not RQ itself. Closing this for now and an issue/question should be raised in a better forum around general React.

  11. tannerlinsley closed this

  12. @tibotiber I ran into the exact same issue today, how did you solve it eventually? It's totally weird useMemo is not memorizing, but I can accept that it's due to the nature of the Suspence

  13. Hi @beshanoe, first thing I gotta say is I'm almost sure Tanner is right and this is not a RQ thing. Probably something to do with hooks, or Suspense. The next version of React has a lot of work that went into Suspense so hopefully things will be working or easier to debug / understand.

    About a fix, I used a workaround that's really specific to my use-case. I needed to memoize a date, so I just made sure it was changing not too frequently by keeping only the start of its ISO String:

    // avoid a new value each second (useMemo didn't work grrrr)
    const today = moment(new Date().toISOString().slice(0, 16))

    This is an ugly fix and it might be irrelevant to you, sorry.

  14. Great pointer @promer94, thank you!

Sign in to join this conversation.