skylerdj
skylerdj2d ago

Is it okay to use a single proxy client in Next.js pages router?

Hey guys quick question if anyone is around. Are there any issues with defining a trpc proxy client once in nextjs pages router and then reusing it in client components by manually passing it to a useQuery?
// define this once in src/trpc.ts
export const trpc = createTRPCProxyClient<AppRouter>({
links: [
httpBatchLink({
url: "my-remote-api-url",
headers: async () => {
// this is coming from a recoil atom
const token = await getToken();
return GetHeaders(token);
},
}),
],
});

// some component or hook in the app
const useMyQuery = () => {
return useQuery({
queryKey: ["my-query"],
queryFn: () => trcp.path.to.endpoint.query(someArgs)
})
}
// define this once in src/trpc.ts
export const trpc = createTRPCProxyClient<AppRouter>({
links: [
httpBatchLink({
url: "my-remote-api-url",
headers: async () => {
// this is coming from a recoil atom
const token = await getToken();
return GetHeaders(token);
},
}),
],
});

// some component or hook in the app
const useMyQuery = () => {
return useQuery({
queryKey: ["my-query"],
queryFn: () => trcp.path.to.endpoint.query(someArgs)
})
}
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?