trpc demo
This commit is contained in:
38
src/app/_trpc/TrpcProvider.tsx
Normal file
38
src/app/_trpc/TrpcProvider.tsx
Normal file
@@ -0,0 +1,38 @@
|
||||
'use client'
|
||||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
||||
import { httpBatchLink } from "@trpc/client";
|
||||
import React, { useState } from "react"
|
||||
import { trpc } from "./Client";
|
||||
|
||||
function getBaseUrl() {
|
||||
if (typeof window !== 'undefined')
|
||||
// browser should use relative path
|
||||
return '';
|
||||
if (process.env.VERCEL_URL)
|
||||
// reference for vercel.com
|
||||
return `https://${process.env.VERCEL_URL}`;
|
||||
if (process.env.RENDER_INTERNAL_HOSTNAME)
|
||||
// reference for render.com
|
||||
return `http://${process.env.RENDER_INTERNAL_HOSTNAME}:${process.env.PORT}`;
|
||||
// assume localhost
|
||||
return `http://localhost:${process.env.PORT ?? 3000}`;
|
||||
}
|
||||
|
||||
export default function TrpcProvider({children}:{children: React.ReactNode}) {
|
||||
const [queryClient] = useState(() => new QueryClient({}));
|
||||
const [trpcClient] = useState(() => {
|
||||
return trpc.createClient({
|
||||
links: [
|
||||
httpBatchLink({
|
||||
url: `${getBaseUrl()}/api/trpc`,
|
||||
}),
|
||||
],
|
||||
})
|
||||
});
|
||||
|
||||
return (
|
||||
<trpc.Provider client={trpcClient} queryClient={queryClient}>
|
||||
<QueryClientProvider client={queryClient}> {children} </QueryClientProvider>
|
||||
</trpc.Provider>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user