T
TanStack10mo ago
optimistic-gold

No QueryClient set, like the example

# main.tsx
imports...

// Set up a Router instance
const router = createRouter({
routeTree,
context: {
queryClient,
},
defaultPreload: 'intent',
// Since we're using React Query, we don't want loader calls to ever be stale
// This will ensure that the loader is always called when the route is preloaded or visited
defaultPreloadStaleTime: 0,
scrollRestoration: true,
})

// Register things for typesafety
declare module '@tanstack/react-router' {
interface Register {
router: typeof router
}
}

const rootElement = document.getElementById('app')!

if (!rootElement.innerHTML) {
const root = ReactDOM.createRoot(rootElement)

root.render(
<QueryClientProvider client={queryClient}>
<RouterProvider router={router} />
</QueryClientProvider>,
)
}
# main.tsx
imports...

// Set up a Router instance
const router = createRouter({
routeTree,
context: {
queryClient,
},
defaultPreload: 'intent',
// Since we're using React Query, we don't want loader calls to ever be stale
// This will ensure that the loader is always called when the route is preloaded or visited
defaultPreloadStaleTime: 0,
scrollRestoration: true,
})

// Register things for typesafety
declare module '@tanstack/react-router' {
interface Register {
router: typeof router
}
}

const rootElement = document.getElementById('app')!

if (!rootElement.innerHTML) {
const root = ReactDOM.createRoot(rootElement)

root.render(
<QueryClientProvider client={queryClient}>
<RouterProvider router={router} />
</QueryClientProvider>,
)
}
#__root.tsx
imports...

export const Route = createRootRouteWithContext<{
queryClient: QueryClient
}>()({
component: RootComponent,
notFoundComponent: () => {
return (
<div className="p-2 border border-red-300">
<p>This is the notFoundComponent configured on root route</p>
</div>
)
},
})

function RootComponent() {
return (
<>
<Outlet />
<ReactQueryDevtools buttonPosition="top-right" />
<TanStackRouterDevtools position="bottom-right" />
</>
)
}
#__root.tsx
imports...

export const Route = createRootRouteWithContext<{
queryClient: QueryClient
}>()({
component: RootComponent,
notFoundComponent: () => {
return (
<div className="p-2 border border-red-300">
<p>This is the notFoundComponent configured on root route</p>
</div>
)
},
})

function RootComponent() {
return (
<>
<Outlet />
<ReactQueryDevtools buttonPosition="top-right" />
<TanStackRouterDevtools position="bottom-right" />
</>
)
}
2 Replies
optimistic-gold
optimistic-goldOP10mo ago
Found the issue, import in the __root.tsx, QueryClient needs to be imported as a type import type { QueryClient } from '@tanstack/react-query'
passive-yellow
passive-yellow10mo ago
Hi how are u/ I am interested on your post. thanks.

Did you find this page helpful?