TrpcContextProvider.tsx 907 B

1234567891011121314151617181920212223242526272829303132
  1. "use client";
  2. import { useState, type ReactNode } from "react";
  3. import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
  4. import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
  5. import { trpc } from "@/lib/trpc";
  6. import { httpBatchLink } from "@trpc/client";
  7. export function TrpcContextProvider({ children }: { children: ReactNode }) {
  8. const [queryClient] = useState(() => new QueryClient({
  9. defaultOptions: {
  10. queries: {
  11. staleTime: 60_000, // 30 seconds
  12. },
  13. },
  14. }))
  15. const [trpcQueryClient] = useState(() => trpc.createClient({
  16. links: [
  17. httpBatchLink({
  18. url: "/api",
  19. }),
  20. ],
  21. }));
  22. return (
  23. <trpc.Provider client={trpcQueryClient} queryClient={queryClient}>
  24. <QueryClientProvider client={queryClient}>
  25. {children}
  26. <ReactQueryDevtools />
  27. </QueryClientProvider>
  28. </trpc.Provider>
  29. );
  30. }