Skip to content
Source
Sign inSign up
TanStack/router

`useAwaited` returns undefined when using `defer` for `queryClient.fetchQuery` #2934

Send to my agent
ClosedHoangDungNg(GitHub profile, opens in a new tab) opened this issue 1 comment

Which project does this relate to?

Router

Describe the bug

I tried to use the defer function with queryClient.fetchQuery. But the <Await> component returned undefined for the resolved data. I have looked at the code of the defer and noticed that when I used the defer function in loader, property status was already added to the promise. Then, the defer function was continuing to be used in the useAwaited hook. This time, the defer function inside the useAwaited hook could not assign property data to the promise because the condition was not met. So the resolved data is undefined. I tried to fix the issue by moving the logic for promise.then out of the condition if (!(promise as any).status). The issue was resolved but I am not sure if it is a legit solution. So, I wrote a customDefer function. It can be found at /utils/customDefer.ts in the codesandbox

Image

Image

Your Example Website or App

https://codesandbox.io/p/devbox/n2jq68

Steps to Reproduce the Bug or Issue

  1. Go to https://codesandbox.io/p/devbox/n2jq68
  2. Notices the right section has the "Loading todos..." text. After 3-4 seconds, the text shows "resolvedTodos is undefined"
  3. The logic for the loader function can be found at /routes/index.tsx

Expected behavior

I expected the useAwaited hook can return the resolved data properly when passing a deferred promise fetched by queryClient

Screenshots or Videos

No response

Platform

  • Browser: Chrome, Microsoft Edge
  • Version: 131.0

Additional context

No response

  1. This is a mistake in the usage.

    The usage of defer here doesn't make sense, since Query is already an asynchronous state management library and therefore mixing it with this primitive is bound to result in an error. When using Query (or a similar library), you want to use its own access patterns for retrieving the fetched data.

    So, in the loader you'll just be kicking off the query and awaiting the getPostsQueryOptions().

    import { createFileRoute } from '@tanstack/router'
    
    export const Route = createFileRoute('/_management/users')({
      loader: async ({ context: { queryClient } }) => {
        queryClient.prefetchQuery(getTodosQueryOptions())
        await queryClient.ensureQueryData(getPostsQueryOptions())
    
       // you don't actually return anything
      },
      component: RouteComponent
    })

    Then in the Route component, you'd be accessing the data using the correct hooks from your async state library of choice. You won't be using Route.useLoaderData().

    import { useSuspenseQuery, useQuery } from '@tanstack/react-query'
    
    function RouteComponent() {
      const posts = useSuspenseQuery(getPostsQueryOptions());
      const todos = useQuery(getTodosQueryOptions());
    
      // ...
    }

    I'll be sure to add a note in the deferred data loading guide, so callout the fact that when you are using an external library for this kind of caching, you'll only be using the loader to kick-off the fetch.

  2. SeanCassiere added labelsdocumentation

  3. SeanCassiere closed this

Sign in to join this conversation.