Skip to content

Commit 459fa8a

Browse files
committed
added provider composer
1 parent df3f762 commit 459fa8a

6 files changed

Lines changed: 31 additions & 16 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"typescript": "~5.9.3",
3838
"typescript-eslint": "^8.50.0",
3939
"vite": "^7.3.0",
40+
"vite-plugin-svgr": "^4.5.0",
4041
"vitest": "^4.0.15"
4142
}
4243
}

src/app/providers/index.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// src/app/providers/index.ts
2+
import { ComponentType } from 'react';
3+
import { withRouter } from './with-router';
4+
5+
type HOC = (component: ComponentType<any>) => ComponentType<any>;
6+
7+
const compose = (...hocs: HOC[]) => {
8+
return (component: ComponentType<any>): ComponentType<any> => {
9+
return hocs.reduceRight((wrapped, hoc) => hoc(wrapped), component);
10+
};
11+
};
12+
13+
export const withProviders = compose(withRouter);

src/app/providers/with-router.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// src/app/providers/with-router.tsx
2+
import { RouterProvider, createRouter } from '@tanstack/react-router';
3+
import { routeTree } from '../../routeTree.gen';
4+
import { ComponentType } from 'react';
5+
6+
const router = createRouter({ routeTree });
7+
8+
export const withRouter = (_Component: ComponentType) => {
9+
return () => <RouterProvider router={router} />;
10+
};

src/app/routes/__root.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { TanStackRouterDevtools } from '@tanstack/react-router-devtools';
55
const RootLayout = () => (
66
<>
77
<Header />
8-
<hr />
98
<Outlet />
109
<TanStackRouterDevtools />
1110
</>

src/main.tsx

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,17 @@
11
import { StrictMode } from 'react';
22
import ReactDOM from 'react-dom/client';
3-
import { RouterProvider, createRouter } from '@tanstack/react-router';
4-
5-
// Import the generated route tree
6-
import { routeTree } from './routeTree.gen';
7-
8-
// Create a new router instance
9-
const router = createRouter({ routeTree });
10-
11-
// Register the router instance for type safety
12-
declare module '@tanstack/react-router' {
13-
interface Register {
14-
router: typeof router;
15-
}
16-
}
3+
import { withProviders } from './app/providers';
174

185
const rootElement = document.getElementById('root')!;
6+
197
if (!rootElement.innerHTML) {
208
const root = ReactDOM.createRoot(rootElement);
9+
10+
const App = withProviders(() => null);
11+
2112
root.render(
2213
<StrictMode>
23-
<RouterProvider router={router} />
14+
<App />
2415
</StrictMode>
2516
);
2617
}

src/vite-env.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/// <reference types="vite/client" />
2+
/// <reference types="vite-plugin-svgr/client" />

0 commit comments

Comments
 (0)