TanStackT
TanStack5mo ago
3 replies
full-green

How to use persistQueryClient in Tanstack Start

Hi, I have been trying to find documentation on using the persistQueryClient in my Tanstack React Start App, but so far have not been able to get it working

For example, consider the Basic + React Query example at https://tanstack.com/start/latest/docs/framework/react/examples/start-basic-react-query

import { QueryClient } from '@tanstack/react-query'
import { createRouter as createTanStackRouter } from '@tanstack/react-router'
import { setupRouterSsrQueryIntegration } from '@tanstack/react-router-ssr-query'
import { routeTree } from './routeTree.gen'
import { DefaultCatchBoundary } from './components/DefaultCatchBoundary'
import { NotFound } from './components/NotFound'

export function createRouter() {
  const queryClient = new QueryClient()

  const router = createTanStackRouter({
    routeTree,
    context: { queryClient },
    defaultPreload: 'intent',
    defaultErrorComponent: DefaultCatchBoundary,
    defaultNotFoundComponent: () => <NotFound />,
  })
  setupRouterSsrQueryIntegration({
    router,
    queryClient,
  })

  return router
}

declare module '@tanstack/react-router' {
  interface Register {
    router: ReturnType<typeof createRouter>
  }
}



How do you exactly integrate this with PersistQueryClientProvider at https://tanstack.com/query/latest/docs/framework/react/plugins/persistQueryClient#persistqueryclientprovider

import { PersistQueryClientProvider } from '@tanstack/react-query-persist-client'
import { createAsyncStoragePersister } from '@tanstack/query-async-storage-persister'

const queryClient = new QueryClient({
  defaultOptions: {
    queries: {
      gcTime: 1000 * 60 * 60 * 24, // 24 hours
    },
  },
})

const persister = createAsyncStoragePersister({
  storage: window.localStorage,
})

ReactDOM.createRoot(rootElement).render(
  <PersistQueryClientProvider
    client={queryClient}
    persistOptions={{ persister }}
  >
    <App />
  </PersistQueryClientProvider>,
)



So far, I'm stuck since I don't understand how the queryClient is declared in the router.tsx file in TSS especially with that ssr integration.

Any help would be appreciated!
An example showing how to implement Start Basic React Query in React using TanStack Start.
React TanStack Start Start Basic React Query Example | TanStack Sta...
Was this page helpful?