Skip to content

Commit 2adbfad

Browse files
committed
fix: changing how we integrate the tanstack query provider
1 parent 1c104a6 commit 2adbfad

File tree

11 files changed

+74
-20
lines changed

11 files changed

+74
-20
lines changed

src/create-app.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ export async function createApp(
442442
}
443443

444444
const integrations: Array<{
445-
type: 'layout' | 'provider' | 'header-user'
445+
type: 'layout' | 'provider' | 'root-provider' | 'header-user'
446446
name: string
447447
path: string
448448
}> = []
@@ -473,6 +473,22 @@ export async function createApp(
473473
path: `integrations/${integration}/provider`,
474474
})
475475
}
476+
if (
477+
environment.exists(
478+
resolve(
479+
targetDir,
480+
'src/integrations',
481+
integration,
482+
'root-provider.tsx',
483+
),
484+
)
485+
) {
486+
integrations.push({
487+
type: 'root-provider',
488+
name: integrationName,
489+
path: `integrations/${integration}/root-provider`,
490+
})
491+
}
476492
if (
477493
environment.exists(
478494
resolve(

templates/react/add-on/tanstack-query/assets/src/integrations/tanstack-query/provider.tsx renamed to templates/react/add-on/tanstack-query/assets/src/integrations/tanstack-query/root-provider.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@ import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
22

33
const queryClient = new QueryClient()
44

5-
export default function Provider({ children }: { children: React.ReactNode }) {
5+
export function getContext() {
6+
return {
7+
queryClient,
8+
}
9+
}
10+
11+
export function Provider({ children }: { children: React.ReactNode }) {
612
return (
713
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
814
)

templates/react/base/README.md.ejs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ const rootRoute = createRootRoute({
152152
Here is an example layout that includes a header:
153153
154154
```tsx
155-
import { createRootRoute, Outlet } from '@tanstack/react-router'
155+
import { Outlet, createRootRoute } from '@tanstack/react-router'
156156
import { TanStackRouterDevtools } from '@tanstack/react-router-devtools'
157157
158158
import { Link } from "@tanstack/react-router";
@@ -214,7 +214,7 @@ mkdir src/routes
214214
Then you'll need to create a `src/routes/__root.<%= jsx %>` file with the contents of the root route that was in `main.<%= jsx %>`.
215215
216216
```tsx
217-
import { createRootRoute, Outlet } from "@tanstack/react-router";
217+
import { Outlet, createRootRoute } from "@tanstack/react-router";
218218
import { TanStackRouterDevtools } from "@tanstack/react-router-devtools";
219219
220220
export const Route = createRootRoute({

templates/react/code-router/src/main.tsx.ejs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import { TanStackRouterDevtools } from "@tanstack/react-router-devtools";
1313
import Header from "./components/Header";
1414
<% } %><% for(const integration of integrations.filter(i => i.type === 'layout' || i.type === 'provider')) { %>
1515
import <%= integration.name %> from "./<%= integration.path %>";
16+
<% } %><% for(const integration of integrations.filter(i => i.type === 'root-provider')) { %>
17+
import * as <%= integration.name %> from "./<%= integration.path %>";
1618
<% } %>
1719

1820
import "./styles.css";
@@ -49,6 +51,11 @@ const routeTree = rootRoute.addChildren([indexRoute<%= routes.map(route => `, ${
4951

5052
const router = createRouter({
5153
routeTree,
54+
context: {
55+
<% for(const integration of integrations.filter(i => i.type === 'root-provider')) { %>
56+
...<%= integration.name %>.getContext(),
57+
<% } %>
58+
},
5259
defaultPreload: "intent",
5360
scrollRestoration: true,
5461
defaultStructuralSharing: true,
@@ -67,7 +74,13 @@ const rootElement = document.getElementById("app");
6774
const root = ReactDOM.createRoot(rootElement);
6875
root.render(
6976
<StrictMode>
70-
<RouterProvider router={router} />
77+
<% for(const integration of integrations.filter(i => i.type === 'root-provider')) { %>
78+
<<%= integration.name %>.Provider>
79+
<% } %>
80+
<RouterProvider router={router} />
81+
<% for(const integration of integrations.filter(i => i.type === 'root-provider').reverse()) { %>
82+
</<%= integration.name %>.Provider>
83+
<% } %>
7184
</StrictMode>
7285
);
7386
}

templates/react/file-router/src/main.tsx.ejs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { StrictMode } from "react";
22
import ReactDOM from "react-dom/client";
33
import { RouterProvider, createRouter } from "@tanstack/react-router";
4+
<% for(const integration of integrations.filter(i => i.type === 'root-provider')) { %>
5+
import * as <%= integration.name %> from "./<%= integration.path %>";
6+
<% } %>
47

58
// Import the generated route tree
69
import { routeTree } from "./routeTree.gen";
@@ -9,7 +12,17 @@ import "./styles.css";
912
import reportWebVitals from "./reportWebVitals.<%= js %>";
1013

1114
// Create a new router instance
12-
const router = createRouter({ routeTree, defaultPreload: "intent", scrollRestoration: true, defaultStructuralSharing: true });
15+
const router = createRouter({
16+
routeTree,
17+
context: {
18+
<% for(const integration of integrations.filter(i => i.type === 'root-provider')) { %>
19+
...<%= integration.name %>.getContext(),
20+
<% } %>
21+
},
22+
defaultPreload: "intent",
23+
scrollRestoration: true,
24+
defaultStructuralSharing: true
25+
});
1326

1427
// Register the router instance for type safety
1528
declare module "@tanstack/react-router" {
@@ -24,7 +37,13 @@ if (rootElement && !rootElement.innerHTML) {
2437
const root = ReactDOM.createRoot(rootElement);
2538
root.render(
2639
<StrictMode>
27-
<RouterProvider router={router} />
40+
<% for(const integration of integrations.filter(i => i.type === 'root-provider')) { %>
41+
<<%= integration.name %>.Provider>
42+
<% } %>
43+
<RouterProvider router={router} />
44+
<% for(const integration of integrations.filter(i => i.type === 'root-provider').reverse()) { %>
45+
</<%= integration.name %>.Provider>
46+
<% } %>
2847
</StrictMode>
2948
);
3049
}

templates/react/file-router/src/routes/__root.tsx.ejs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { createRootRoute, Outlet<% if (addOnEnabled.start) { %>
2-
,HeadContent, Scripts<% } %> } from '@tanstack/react-router'
1+
import { Outlet<% if (addOnEnabled.start) { %>
2+
, HeadContent, Scripts<% } %>, createRootRoute } from '@tanstack/react-router'
33
import { TanStackRouterDevtools } from '@tanstack/react-router-devtools'
44
<% if (addOns.length) { %>
55
import Header from '../components/Header'

templates/solid/base/README.md.ejs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ const rootRoute = createRootRoute({
131131
Here is an example layout that includes a header:
132132
133133
```tsx
134-
import { createRootRoute, Outlet } from '@tanstack/solid-router'
134+
import { Outlet, createRootRoute } from '@tanstack/solid-router'
135135
import { TanStackRouterDevtools } from '@tanstack/router-devtools'
136136
137137
import { Link } from "@tanstack/solid-router";

tests/snapshots/cra/cr-js-npm.json

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

tests/snapshots/cra/cr-ts-npm.json

Lines changed: 2 additions & 2 deletions
Large diffs are not rendered by default.

tests/snapshots/cra/fr-ts-npm.json

Lines changed: 3 additions & 3 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)