What's the difference between the `api` exported from react.tsx and the `api` from server.ts?

I'm using Next, tRPC, & prisma. In the boilerplate, there's two api objects. One is created with createTRPCReact, and the other created with createHydrationHelpers. What's the difference in what they do? In the examples on StackBlitz there's only one, and it's created with createTRPCNext: https://stackblitz.com/run?file=example-app%2Fsrc%2Fpages%2Findex.tsx,example-app%2Fsrc%2Futils%2Fapi.ts
StackBlitz | Instant Dev Environments | Click. Code. Done.
StackBlitz is the collaborative browser-based IDE for web developers. StackBlitz eliminates time-consuming local configuration and lets developers spend more time building.
Solution:
the api from react.tsx is meant for the client with the tanstack-query bindings while the api exported from server.tsx is meant for use within the server only (async components and server functions)
Jump to solution
6 Replies
Solution
Zougui
Zougui3w ago
the api from react.tsx is meant for the client with the tanstack-query bindings while the api exported from server.tsx is meant for use within the server only (async components and server functions)
ChrisEvans
ChrisEvansOP3w ago
@Zougui thanks 🙏 so if I'm calling api.posts.<whatever> in my component and that api object is coming from ~/trpc/server, that's incorrect?
Zougui
Zougui3w ago
if it's an async component, like one that is exported from a page.tsx in the app folder, yes. do keep in mind that the functions in the api exported from server.tsx are async. if your component is not async then no, you'll have to use the api from react.tsx
ChrisEvans
ChrisEvansOP3w ago
And for async components we can import directly from server.tsx?
Zougui
Zougui3w ago
yes
ChrisEvans
ChrisEvansOP3w ago
thank you 🙏

Did you find this page helpful?