0
Fork 0
mirror of https://github.com/TanStack/router.git synced 2026-11-16 19:38:07 +00:00
router/examples/react/router-monorepo-react-query
History 2025-09-11 21:44:48 +00:00
..
.vscode chore(examples): add .vscode/settings.json configuration (#3349) 2025-02-07 10:29:48 +13:00
assets docs: add monorepo with simple and react query example (#2479) 2024-10-18 23:51:23 +02:00
packages release: v1.132.0-alpha.21 2025-09-11 21:44:48 +00:00
.gitignore chore: remove generated js from monorepo example (#2922) 2024-12-04 17:25:11 +01:00
.stackblitzrc chore(docs): update monorepo example to use .stackblitzrc file (#2611) 2024-10-21 22:13:33 +02:00
package.json release: v1.132.0-alpha.21 2025-09-11 21:44:48 +00:00
pnpm-workspace.yaml.example chore(docs): update monorepo example to use .stackblitzrc file (#2611) 2024-10-21 22:13:33 +02:00
README.md docs: add monorepo with simple and react query example (#2479) 2024-10-18 23:51:23 +02:00

Example of a monorepo with router and feature libraries

To run this example:

  • npm install or yarn
  • npm dev or yarn dev

A challenge with TanStack Router in a monorepo setup is that it requires TypeScript type augmentations. However, if you set this up directly in the final app, the links inside the libraries won’t be type-safe. To solve this in a monorepo, you need a separate library just for the router, without any components, and then integrate it with the app.

This example showcases this approach using the following packages:

  • packages/router is the router library
  • packages/post-query is the post query collection library
  • packages/post-feature is the posts ui library
  • packages/app is the app

With this approach, we can use the query options from the data library both in the router and the feature library without creating circular dependencies.

Since the router library re-exports the router components, importing them in the feature library ensures they remain type-safe, as they’re linked to the TypeScript augmentations.

Finally, in the app, we can create a map of routes to components (packages/app/src/main.tsx), which ties the router to the components. We could enforce lazy loading here, but it was left out for simplicity. With this setup, we now have a fully type-safe router!

Here is what it looks like in the monorepo:

graph

Stackblitz limitation

TypeScript IDE feedback

Due to a limitation on Stackblitz, the example's types are not properly inferred in the IDE, however as soon as you click on fork in the bottom right corner, the types should be correctly inferred.