trpc demo

This commit is contained in:
2025-04-12 01:49:57 +02:00
parent 41059d9396
commit 10d1c91dea
15 changed files with 255 additions and 30 deletions

View 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>
)
}