1- import { useState } from "react" ;
2- import { QueryClient , QueryClientProvider } from "@tanstack/react-query" ;
3- import { httpBatchLink , loggerLink } from "@trpc/client" ;
4- import { createTRPCReact } from "@trpc/react-query" ;
1+ import { QueryClient } from "@tanstack/react-query" ;
2+ import { createTRPCClient , httpBatchLink } from "@trpc/client" ;
53import { inferRouterOutputs } from "@trpc/server" ;
4+ import { createTRPCOptionsProxy } from "@trpc/tanstack-react-query" ;
65import Constants from "expo-constants" ;
76
87import type { TRPCRouter } from "../app/api/trpc/[trpc]+api" ;
98
9+ export const queryClient = new QueryClient ( {
10+ defaultOptions : {
11+ queries : {
12+ // ...
13+ } ,
14+ } ,
15+ } ) ;
16+
1017/**
11- * A set of typesafe hooks for consuming your API .
18+ * A type-safe proxy for working with the React Query client .
1219 */
13- export const trpc = createTRPCReact < TRPCRouter > ( ) ;
20+ export const trpc = createTRPCOptionsProxy < TRPCRouter > ( {
21+ client : createTRPCClient ( {
22+ links : [
23+ // loggerLink({
24+ // enabled: (opts) =>
25+ // process.env.NODE_ENV === "development" ||
26+ // (opts.direction === "down" && opts.result instanceof Error),
27+ // colorMode: "ansi",
28+ // }),
29+ httpBatchLink ( { url : resolveUrl ( ) } ) ,
30+ ] ,
31+ } ) ,
32+ queryClient,
33+ } ) ;
1434
1535export type RouterOutputs = inferRouterOutputs < TRPCRouter > ;
1636
1737/**
1838 * Extend this function when going to production by
1939 * setting the baseUrl to your production API URL.
2040 */
21- const resolveUrl = ( ) => {
41+ function resolveUrl ( ) {
2242 /**
2343 * Gets the IP address of your host-machine. If it cannot automatically find it,
2444 * you'll have to manually set it. NOTE: Port 3000 should work for most but confirm
@@ -41,33 +61,4 @@ const resolveUrl = () => {
4161 `Failed to resolve URL from ${ process . env . EXPO_PUBLIC_SERVER_ORIGIN } or ${ debuggerHost } ` ,
4262 ) ;
4363 }
44- } ;
45-
46- /**
47- * A wrapper for your app that provides the TRPC context.
48- * Use only in _app.tsx
49- */
50- export function TRPCProvider ( props : { children : React . ReactNode } ) {
51- const [ queryClient ] = useState ( ( ) => new QueryClient ( ) ) ;
52- const [ trpcClient ] = useState ( ( ) =>
53- trpc . createClient ( {
54- links : [
55- // loggerLink({
56- // enabled: (opts) =>
57- // process.env.NODE_ENV === "development" ||
58- // (opts.direction === "down" && opts.result instanceof Error),
59- // colorMode: "ansi",
60- // }),
61- httpBatchLink ( { url : resolveUrl ( ) } ) ,
62- ] ,
63- } ) ,
64- ) ;
65-
66- return (
67- < trpc . Provider client = { trpcClient } queryClient = { queryClient } >
68- < QueryClientProvider client = { queryClient } >
69- { props . children }
70- </ QueryClientProvider >
71- </ trpc . Provider >
72- ) ;
7364}
0 commit comments