Skip to content
Source
Sign inSign up
TanStack/query

Can't use QueryClient in server component #8698

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

Describe the bug

I'm using NextJS 15.1.7 and @tanstack/react-query 5.66.9

I keep getting this error when I remove the 'use client' directive from the provider component. I have a separate component for providers wherein I have define the QueryClientProvider, but if I don't declare it as client component, it keeps breaking and gives this error.

What am I doing wrong here? I believe we can use it in server components as well

Image

Your minimal, reproducible example

Couldn't get this working with Nextjs

Steps to reproduce

"use client";

import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { PropsWithChildren } from "react";


const queryClient = new QueryClient();

export default function TanstackQueryProvider({ children }: Readonly<PropsWithChildren>) {

  return (
    <QueryClientProvider client={queryClient}>
      {children}
    </QueryClientProvider>
  );
}

I'm using this component to wrap the children in my app layout file

Expected behavior

I expected it to work without errors in a server component, but I keep getting this error unless I use the 'use client" directive

Image

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

  • OS: Ubuntu 22.04.5 LTS x86_64
  • Browser: Firefox Dev Edition
  • Browser Version: Mozilla Firefox 136.0b9

Tanstack Query adapter

react-query

TanStack Query version

5.66.9

TypeScript version

5.7.2

Additional context

No response

  1. The QueryClient is a class, which is something that nextJs can’t serialize. You have to define the QueryClient in a client component.

  2. TkDodo closed this

  3. TkDodo added labelsinvalid

Sign in to join this conversation.