Skip to content

Commit

Permalink
Add tRPC provider for frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
ruchernchong committed Feb 6, 2025
1 parent 584f151 commit 35d8b5b
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/providers/trpc.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { httpBatchLink } from '@trpc/client';
import { createTRPCReact } from '@trpc/react-query';
import { useState } from 'react';
import type { AppRouter } from '@sgcarstrends/api/src/trpc/router';

export const trpc = createTRPCReact<AppRouter>();

export function TrpcProvider({ children }: { children: React.ReactNode }) {
const [queryClient] = useState(() => new QueryClient());
const [trpcClient] = useState(() =>
trpc.createClient({
links: [
httpBatchLink({
url: process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3000/trpc',
}),
],
})
);

return (
<trpc.Provider client={trpcClient} queryClient={queryClient}>
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
</trpc.Provider>
);
}

0 comments on commit 35d8b5b

Please sign in to comment.