You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
when adding react-query to next 13 i get following error when running
error - ./node_modules/@tanstack/react-query/build/lib/Hydrate.mjs
Attempted import error: 'useRef' is not exported from 'react' (imported as 'React').
Import trace for requested module:
./node_modules/@tanstack/react-query/build/lib/Hydrate.mjs
./node_modules/@tanstack/react-query/build/lib/index.mjs
./src/app/layout.tsx
code
import {QueryClient, QueryClientProvider} from "@tanstack/react-query";
const queryClient = new QueryClient()
/* ... */
<QueryClientProvider client={queryClient}>
{children}
</QueryClientProvider>
/* ... */
With Next.js 13's app directory feature, every component, by default, is a Server Component. Which means you are trying to use context in a Server Component, which is not supported. Instead, you should create a new Client Component (with 'use client'; directive), apply your context provider there (alongside any other context providers), and import that in your root layout.
exportdefaultfunctionRootLayout({
children,}: {children: React.ReactNode}){return(<htmllang="en">{/* <head /> will contain the components returned by the nearest parent head.tsx. Find out more at https://beta.nextjs.org/docs/api-reference/file-conventions/head */}<head/><body><QueryProvider>{children}</QueryProvider></body></html>)}
Reacted by Yosef Herskovitz, Dominik Dorfmeister 🔮, Dylan Stein, Usman Khalil, Abdulhamit Akaslan, Oliver Meyer, Yuhwan Kim, Arshad Chowdhury, Wei, AbobTech and 21 more
Reacted by hashtagobi, Jude Francis, yannJaster10i9, Mustafa Dalga, seanimamo, Colin Eckert and JiongXin
I am seeing the same error. The TanStack docs suggest creating a singleton client on the server for import into server components, but that does not seem to work.
I do not get the error with the initialData approach outlined in the docs, presumably because it does not create a client on the server. I am using Next 13.1.6 and react-query 4.24.4.
I also cannot get it working with Hydrate, following the docs. It does work with the initialData.
I made a sandbox with a very simple Next 13 app. I made two examples, one with initial data, and one with hydrate, by following the TanStack docs for Next 13. The hydrate code throws the error.
The docs say you have to put the providers into a separate app/providers.tsx file that has 'use client' at the top. I did that and it works, here's a fork of your reproduction:
The docs say you have to put the providers into a separate app/providers.tsx file that has 'use client' at the top. I did that and it works, here's a fork of your reproduction:
Yes, it works up to now but once you start using getQueryClient in server component you get the error error - ../../node_modules/@tanstack/react-query/build/lib/Hydrate.mjs Attempted import error: 'useRef' is not exported from 'react' (imported as 'React').
@Ephem do you know why this is happening? It seems like
import { QueryClient } from "@tanstack/react-query"; brings in a lot more than just the QueryClient, as the error comes form Hydrate.tsx. Removing that import makes it work. However, Hydrate.tsx alone has the 'use client' directive at the top 🤔
@TkDodo If you go to the Hydrate.esm.js output the 'use client' is not there for some reason, has it been removed during the build process? I am using v4.24.4.
I finally did some digging and thinking on this today and thought I'd document my findings:
"use client" wont ever work in a bundled build with shared code. Either you include it at the top of the bundle for everything, or you don't. My suggestion:
We require/strongly recommend you use an unbundled build if you are using RSCs (this is what Next will use out of the box, so should be straightforward)
If we would want to have a workaround so bundled builds could work with RSCs, we could use the Rollup banner2 plugin to add "use client" at the top of the bundled builds, and document that in this case you need to do imports in RSCs from @tanstack/query-core instead of @tanstack/react-query. Not sure if it would work, but even if it did, I don't think this is something we should really support unless some very clear need arises.
My hope is that preserving directives could be supported when the option preserveModules: true is set, that would let us fix this issue so everything works as in the current documentation, left a comment about this in the issue.
Another reason builds are failing is even if you fix the above, builds are now(? Has this always been the case? Feels like something has changed here..) failing on imports and not usage of React "client" features, builds will fail on any server import of @tanstack/react-query since the index file is re-exporting things like useQuery that has a bunch of client-only features in it, but no "use client" at the top.
Note that not having "use client" at the top of those was intentional to have better error messages, these should never be imported from a server component, so only having "use client" at the top of component files gave better messages. I tested this myself, this is why it feels as if something has changed here..
Either way, to fix this we should just add back "use client" to the top of every file that uses React client only features in the react-query package.
I think we should:
While we wait for the Rollup issue to unblock: Update the docs to the workaround @feledori documented above (basically what the docs @efilion contributed were before we added "use client" and rewrote them to remove HydrateOnClient).
Add "use client" to the top of all files in @tanstack/react-query that imports something from React (all of them besides index?) (This wont fix anything until the Rollup issue is unblocked though)
Actually, what we could do now as a temporary fix is add "use client" to the top of the entire react-query package with the Rollup banner2-plugin as documented in that issue (maybe only for the .mjs build?) and just document that on the server you have to use @tanstack/query-core for now? As long as you always use that only in RSCs, and always import from @tanstack/react-query elsewhere we shouldn't have a problem with duplicates right?
I just found the fix, In my case I was using one valtio function in utils.ts file which was used somewhere in the server side api.
Rule is if you're using any client side library then make sure that you're not using it's client apis/functions of library directly or indirectly on server routes or server actions.
1. Wrapping the Hydrate wrapper in a separate "use client" component
2. Importing QueryClient from @tanstack/query-core in the server singleton
3. Importing the dehydrate function from @tanstack/query-core as well.
@barbarah Here is the working sandbox with the changes I just mentioned.
This worked, and this is an outline of what I am using
Activity
Bknight010723 commentedon Feb 4, 2023
sdkmanager --uninstall --package_file #4939
d4nyll commentedon Feb 5, 2023
With Next.js 13's
appdirectory feature, every component, by default, is a Server Component. Which means you are trying to use context in a Server Component, which is not supported. Instead, you should create a new Client Component (with'use client';directive), apply your context provider there (alongside any other context providers), and import that in your root layout.See Using context in Client Components for more details.
carrithers commentedon Feb 8, 2023
I am seeing the same error. The TanStack docs suggest creating a singleton client on the server for import into server components, but that does not seem to work.
I do not get the error with the
initialDataapproach outlined in the docs, presumably because it does not create a client on the server. I am using Next13.1.6and react-query4.24.4.arzzzen commentedon Feb 8, 2023
+1 have the same error after attempt to follow TanStack docs
barbarah commentedon Feb 9, 2023
I also cannot get it working with Hydrate, following the docs. It does work with the
initialData.I made a sandbox with a very simple Next 13 app. I made two examples, one with initial data, and one with hydrate, by following the TanStack docs for Next 13. The hydrate code throws the error.
https://codesandbox.io/p/sandbox/next13-react-query-h4qh77
TkDodo commentedon Feb 12, 2023
The docs say you have to put the providers into a separate
app/providers.tsxfile that has'use client'at the top. I did that and it works, here's a fork of your reproduction:https://codesandbox.io/p/sandbox/gracious-merkle-8lzrrk
CogitoBurrito commentedon Feb 14, 2023
Yes, it works up to now but once you start using getQueryClient in server component you get the error
error - ../../node_modules/@tanstack/react-query/build/lib/Hydrate.mjs Attempted import error: 'useRef' is not exported from 'react' (imported as 'React').CogitoBurrito commentedon Feb 14, 2023
@TkDodo
demo
TkDodo commentedon Feb 14, 2023
@Ephem do you know why this is happening? It seems like
import { QueryClient } from "@tanstack/react-query";brings in a lot more than just theQueryClient, as the error comes formHydrate.tsx. Removing that import makes it work. However,Hydrate.tsxalone has the'use client'directive at the top 🤔CogitoBurrito commentedon Feb 14, 2023
@TkDodo If you go to the Hydrate.esm.js output the
'use client'is not there for some reason, has it been removed during the build process? I am using v4.24.4.CogitoBurrito commentedon Feb 14, 2023
Can't upload img...
module path:
node_modules/@tanstack/react-query/build/lib/Hydrate.esm.jsline 1:
import * as React from 'react';source code path:
react-query/src/Hydrate.tsxline 1:
'use client'line 2:
import * as React from 'react'TkDodo commentedon Feb 14, 2023
I think it could be this:
probably we'd need to preserve the directive for esm builds?
CogitoBurrito commentedon Feb 15, 2023
The way suggested will add
'use client'to every single file isn't it? Would it be harmful?5 remaining items
barbarah commentedon Feb 16, 2023
Sorry, no. I've tried the different options mentioned above. But I did not get my sandbox working.
I did update the sandbox link because apparently, the link was not working: https://codesandbox.io/p/sandbox/next13-react-query-h4qh77
feledori commentedon Feb 23, 2023
I got it working by doing these step:
@barbarah Here is the working sandbox with the changes I just mentioned.
mk965 commentedon Mar 10, 2023
@feledori
Good job!
But in your demo "getPost" is run twice. Once on the server, once on the client.
feledori commentedon Mar 10, 2023
@mk965 Just updated the sandbox, you just have to disable revalidateOnMount for the hydrated useQuery hook.
Ephem commentedon Mar 11, 2023
I finally did some digging and thinking on this today and thought I'd document my findings:
"use client"wont ever work in a bundled build with shared code. Either you include it at the top of the bundle for everything, or you don't. My suggestion:@tanstack/query-coreinstead of@tanstack/react-query. Not sure if it would work, but even if it did, I don't think this is something we should really support unless some very clear need arises.TheOne reason our non-bundled builds are currently failing on this is indeed the Rollup issue @TkDodo linked earlier: 'use client' directive for Client Components rollup/rollup#4699preserveModules: trueis set, that would let us fix this issue so everything works as in the current documentation, left a comment about this in the issue.@tanstack/react-querysince the index file is re-exporting things likeuseQuerythat has a bunch of client-only features in it, but no"use client"at the top."use client"at the top of those was intentional to have better error messages, these should never be imported from a server component, so only having"use client"at the top of component files gave better messages. I tested this myself, this is why it feels as if something has changed here.."use client"to the top of every file that uses React client only features in thereact-querypackage.I think we should:
HydrateOnClient)."use client"to the top of all files in@tanstack/react-querythat imports something from React (all of them besides index?) (This wont fix anything until the Rollup issue is unblocked though)Ephem commentedon Mar 11, 2023
Actually, what we could do now as a temporary fix is add
"use client"to the top of the entire react-query package with the Rollup banner2-plugin as documented in that issue (maybe only for the .mjs build?) and just document that on the server you have to use@tanstack/query-corefor now? As long as you always use that only in RSCs, and always import from@tanstack/react-queryelsewhere we shouldn't have a problem with duplicates right?Ephem commentedon Mar 12, 2023
I got nerdsniped and built a Rollup plugin to preserve directives: https://github.com/Ephem/rollup-plugin-preserve-directives
I'll do some more testing, but with this I think we should be able to make it all work as per the docs.
TkDodo commentedon Mar 20, 2023
please try it with:
mahdi-farnia commentedon Apr 10, 2023
I did every thing including Provider file that has 'use client' directive and everything i found,
But it didn't work
Does this occurs because of next 13.3.0? (provided examples in this thread are using version 13.1.x)
2manoj1 commentedon Apr 13, 2023
Hello Everyone!
I made small POC with Next13 and React Query - Its working.
Have a look
Example code
Need a feedback or suggestion if any
momenfc commentedon May 16, 2023
try to import dehydrate from '@tanstack/query-core' instead '@tanstack/react-query'
import { dehydrate } from '@tanstack/query-core';
Schmale97 commentedon May 30, 2023
I have also been able to get it to work with next version >=13.3.0 and @tanstack/* version 4.29.12
Nishchit14 commentedon Jan 31, 2024
I just found the fix, In my case I was using one
valtiofunction inutils.tsfile which was used somewhere in the server side api.Rule is if you're using any client side library then make sure that you're not using it's client apis/functions of library directly or indirectly on server routes or server actions.
Franklivania commentedon Apr 10, 2024
This worked, and this is an outline of what I am using