Skip to content

Commit ac8d561

Browse files
committed
fix: tanstack query integration
1 parent 40b9c12 commit ac8d561

3 files changed

Lines changed: 30 additions & 34 deletions

File tree

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

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,7 @@ export const trpcClient = createTRPCClient<TRPCRouter>({
2525
],
2626
});
2727
28-
let context:
29-
| {
30-
queryClient: QueryClient
31-
trpc: ReturnType<typeof createTRPCOptionsProxy<TRPCRouter>>
32-
}
33-
| undefined
34-
3528
export function getContext() {
36-
if (context) {
37-
return context
38-
}
39-
4029
const queryClient = new QueryClient({
4130
defaultOptions: {
4231
dehydrate: { serializeData: superjson.serialize },
@@ -58,10 +47,12 @@ export function getContext() {
5847
5948
export default function TanStackQueryProvider({
6049
children,
50+
context,
6151
}: {
62-
children: ReactNode
52+
children: ReactNode,
53+
context: ReturnType<typeof getContext>
6354
}) {
64-
const { queryClient } = getContext()
55+
const { queryClient } = context
6556
6657
return (
6758
<QueryClientProvider client={queryClient}>
@@ -75,32 +66,22 @@ export default function TanStackQueryProvider({
7566
import type { ReactNode } from 'react'
7667
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
7768
78-
let context:
79-
| {
80-
queryClient: QueryClient
81-
}
82-
| undefined
83-
8469
export function getContext() {
85-
if (context) {
86-
return context
87-
}
88-
8970
const queryClient = new QueryClient();
9071
91-
context = {
72+
return {
9273
queryClient,
9374
}
94-
95-
return context
9675
}
9776
9877
export default function TanStackQueryProvider({
9978
children,
79+
context,
10080
}: {
10181
children: ReactNode
82+
context: ReturnType<typeof getContext>
10283
}) {
103-
const { queryClient } = getContext()
84+
const { queryClient } = context
10485
10586
return (
10687
<QueryClientProvider client={queryClient}>

packages/create/src/frameworks/react/add-ons/tanstack-query/info.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@
1818
}
1919
],
2020
"integrations": [
21-
{
22-
"type": "provider",
23-
"path": "src/integrations/tanstack-query/root-provider.tsx",
24-
"jsName": "TanStackQueryProvider"
25-
},
2621
{
2722
"type": "devtools",
2823
"path": "src/integrations/tanstack-query/devtools.tsx",

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,42 @@
11
import { createRouter as createTanStackRouter } from '@tanstack/react-router'
22
import { routeTree } from './routeTree.gen'
33
<% if (addOnEnabled['tanstack-query']) { %>
4-
import { getContext } from './integrations/tanstack-query/root-provider'
4+
import { setupRouterSsrQueryIntegration } from "@tanstack/react-router-ssr-query";
5+
import TanStackQueryProvider, {
6+
getContext,
7+
} from "./integrations/tanstack-query/root-provider";
58
<% } %>
69

710
export function getRouter() {
11+
<% if (addOnEnabled['tanstack-query']) { %>
12+
const context = getContext();
13+
<% } %>
14+
815
const router = createTanStackRouter({
916
routeTree,
1017
<% if (addOnEnabled['tanstack-query']) { %>
11-
context: getContext(),
18+
context,
1219
<% } else if (addOnEnabled['apollo-client']) { %>
1320
context: {} as any,
1421
<% } %>
1522
scrollRestoration: true,
1623
defaultPreload: 'intent',
1724
defaultPreloadStaleTime: 0,
25+
<% if (addOnEnabled['tanstack-query']) { %>
26+
Wrap: (props: { children: React.ReactNode }) => {
27+
return (
28+
<TanStackQueryProvider context={context}>
29+
{props.children}
30+
</TanStackQueryProvider>
31+
);
32+
},
33+
<% } %>
1834
})
1935

36+
<% if (addOnEnabled['tanstack-query']) { %>
37+
setupRouterSsrQueryIntegration({ router, queryClient: context.queryClient })
38+
<% } %>
39+
2040
return router
2141
}
2242

0 commit comments

Comments
 (0)