Skip to content

Commit 46ec412

Browse files
committed
fix: trpc integration
1 parent 8f9fcc3 commit 46ec412

2 files changed

Lines changed: 31 additions & 11 deletions

File tree

packages/create/src/frameworks/react/add-ons/tanstack-query/assets/src/integrations/tanstack-query/root-provider.tsx.ejs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,15 @@ export function getContext() {
3737
client: trpcClient,
3838
queryClient: queryClient,
3939
});
40-
context = {
40+
const context = {
4141
queryClient,
4242
trpc: serverHelpers,
4343
}
4444
4545
return context
4646
}
4747
48-
export default function TanStackQueryProvider({
48+
export default function TanstackQueryProvider({
4949
children,
5050
context,
5151
}: {
@@ -55,11 +55,20 @@ export default function TanStackQueryProvider({
5555
const { queryClient } = context
5656
5757
return (
58-
<QueryClientProvider client={queryClient}>
59-
<TRPCProvider trpcClient={trpcClient} queryClient={queryClient}>
60-
{children}
61-
</TRPCProvider>
62-
</QueryClientProvider>
58+
<TRPCProvider trpcClient={trpcClient} queryClient={queryClient}>
59+
{children}
60+
</TRPCProvider>
6361
)
6462
}
63+
<% } else { %>
64+
import { QueryClient } from '@tanstack/react-query'
65+
66+
export function getContext() {
67+
const queryClient = new QueryClient();
68+
69+
return {
70+
queryClient,
71+
}
72+
}
73+
export default function TanstackQueryProvider() {}
6574
<% } %>

packages/create/src/frameworks/react/project/base/src/router.tsx.ejs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,38 @@
11
import { createRouter as createTanStackRouter } from '@tanstack/react-router'
22
import { routeTree } from './routeTree.gen'
33
<% if (addOnEnabled['tanstack-query']) { %>
4+
import type { ReactNode } from 'react'
45
import { QueryClient } from '@tanstack/react-query'
56
import { setupRouterSsrQueryIntegration } from "@tanstack/react-router-ssr-query";
7+
import TanstackQueryProvider, { getContext } from './integrations/tanstack-query/root-provider'
68
<% } %>
79

810
export function getRouter() {
911
<% if (addOnEnabled['tanstack-query']) { %>
10-
const queryClient = new QueryClient();
12+
const context = getContext();
1113
<% } %>
12-
14+
1315
const router = createTanStackRouter({
1416
routeTree,<% if (addOnEnabled['tanstack-query']) { %>
15-
context: { queryClient },<% } else if (addOnEnabled['apollo-client']) { %>
17+
context,<% } else if (addOnEnabled['apollo-client']) { %>
1618
context: {} as any,
1719
<% } %>
1820
scrollRestoration: true,
1921
defaultPreload: 'intent',
2022
defaultPreloadStaleTime: 0,
23+
<% if (addOnEnabled.tRPC) { %>
24+
Wrap: (props: { children: ReactNode }) => {
25+
return (
26+
<TanstackQueryProvider context={context}>
27+
{props.children}
28+
</TanstackQueryProvider>
29+
);
30+
},
31+
<% } %>
2132
})
2233

2334
<% if (addOnEnabled['tanstack-query']) { %>
24-
setupRouterSsrQueryIntegration({ router, queryClient: queryClient })
35+
setupRouterSsrQueryIntegration({ router, queryClient: context.queryClient })
2536
<% } %>
2637

2738
return router

0 commit comments

Comments
 (0)