tRPC & Next.js confused about error
Hey, I'm really new with next.js, react and trpc, I was trying to add tRPC into my small SSR next.js project to send data from the client to the server, following the tRPC Next.js examples is pretty tricky knowing close to nothing about it and I am getting a weird error. It doesn't tell me where it is, what file, I've tried mostly commenting out code from my
page.tsx
file and I still get the useState error? I'm not sure how to set up layout.tsx
too... That's probably what I'm doing wrong but I don't know where to find out what is wrong
layout.tsx
Solution:Jump to solution
For context, trpc does not officially support Nextjs App router. Instead of wrapping your component in trpc's HoC, you can create a client component provider and wrap your app with it.
Look here https://github.com/trpc/trpc/issues/3297#issuecomment-1423905894...
GitHub
feat: Support RSC & App Layouts · Issue #3297 · trpc/trpc
Describe the feature you'd like to request We should make official support for Next 13 app layouts & RSC. Describe the solution you'd like to see Being able to transparently use tRPC id...
8 Replies
try to read about server components. for context, all components under app/ are server components, adding interactivity such as useState requires it to be a client component. that is why it says you need to add a "use client" at the top of your component file
There are no client interactivity elements in the app directory from what I've checked, I've even removed everything from
page.tsx
and it still shows the error
page.tsx
adding a side effect (such as hooks) is interactivity
server components are compiled server side, so adding a hook on that will throw an error
There are no hooks in my code, the error keeps showing up
Full error
The error isn't helping me find the useState that isn't anywhere
How about in layout.tsx? Are you using trpc? Have you checked if removing the trpc provider removes the error?
yes, it appears the error is not there when I comment out the trpc
layout.tsx
and uncomment the original next.js template code
layout.tsx
Solution
For context, trpc does not officially support Nextjs App router. Instead of wrapping your component in trpc's HoC, you can create a client component provider and wrap your app with it.
Look here https://github.com/trpc/trpc/issues/3297#issuecomment-1423905894
GitHub
feat: Support RSC & App Layouts · Issue #3297 · trpc/trpc
Describe the feature you'd like to request We should make official support for Next 13 app layouts & RSC. Describe the solution you'd like to see Being able to transparently use tRPC id...
Thanks a million! I got it fixed despite delay