Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

devtools.md

179 lines5.1 KB

1---
2title: Devtools
3---
4​
5> Link, take this sword... I mean Devtools!... to help you on your way!
6​
7Wave your hands in the air and shout hooray because TanStack Router comes with dedicated devtools! πŸ₯³
8​
9When you begin your TanStack Router journey, you'll want these devtools by your side. They help visualize all of the inner workings of TanStack Router and will likely save you hours of debugging if you find yourself in a pinch!
10​
11> Please note that for now, the devtools **are only supported in React**. If you would like to help us make the devtools platform-agnostic, please let us know!
12​
13## Installation
14​
15The devtools are a separate package that you need to install:
16​
17```sh
18npm install -D @tanstack/router-devtools
19```
20​
21or
22​
23```sh
24pnpm add -D @tanstack/router-devtools
25```
26​
27or
28​
29```sh
30yarn add -D @tanstack/router-devtools
31```
32​
33or
34​
35```sh
36bun add -D @tanstack/router-devtools
37```
38​
39## Import the Devtools
40​
41```js
42import { TanStackRouterDevtools } from '@tanstack/router-devtools'
43```
44​
45## Only importing and using Devtools in Development
46​
47To do this, simply use `React.lazy` and the env variable of your choice to optionally return a dummy component:
48​
49```tsx
50const TanStackRouterDevtools =
51 process.env.NODE_ENV === 'production'
52 ? () => null // Render nothing in production
53 : React.lazy(() =>
54 // Lazy load in development
55 import('@tanstack/router-devtools').then((res) => ({
56 default: res.TanStackRouterDevtools,
57 // For Embedded Mode
58 // default: res.TanStackRouterDevtoolsPanel
59 })),
60 )
61```
62​
63Then wrap the `TanStackRouterDevtools` component in suspense.
64​
65```tsx
66<Suspense>
67 <TanStackRouterDevtools />
68</Suspense>
69```
70​
71## Using inside of the `RouterProvider`
72​
73The easiest way for the devtools to work is to render them inside of your root route (or any other route). This will automatically connect the devtools to the router instance.
74​
75```tsx
76const rootRoute = createRootRoute({
77 component: () => (
78 <>
79 <Outlet />
80 <TanStackRouterDevtools />
81 </>
82 ),
83})
84​
85const routeTree = rootRoute.addChildren([
86 // ... other routes
87])
88​
89const router = createRouter({
90 routeTree,
91})
92​
93function App() {
94 return <RouterProvider router={router} />
96```
97​
98## Manually passing the Router Instance
99​
100If rendering the devtools inside of the `RouterProvider` isn't your cup of tea, a `router` prop for the devtools accepts the same `router` instance you pass to the `Router` component. This makes it possible to place the devtools anywhere on the page, not just inside the provider:
101​
102```tsx
103function App() {
104 return (
105 <>
106 <RouterProvider router={router} />
107 <TanStackRouterDevtools router={router} />
108 </>
109 )
111```
112​
113## Floating Mode
114​
115Floating Mode will mount the devtools as a fixed, floating element in your app and provide a toggle in the corner of the screen to show and hide the devtools. This toggle state will be stored and remembered in localStorage across reloads.
116​
117Place the following code as high in your React app as you can. The closer it is to the root of the page, the better it will work!
118​
119```js
120import { TanStackRouterDevtools } from '@tanstack/router-devtools'
121​
122function App() {
123 return (
124 <>
125 <Router />
126 <TanStackRouterDevtools initialIsOpen={false} />
127 </>
128 )
130```
131​
132### Options
133​
134- `router: Router`
135 - The router instance to connect to
136- `initialIsOpen: Boolean`
137 - Set this `true` if you want the devtools to default to being open
138- `panelProps: PropsObject`
139 - Use this to add props to the panel. For example, you can add `className`, `style` (merge and override default style), etc.
140- `closeButtonProps: PropsObject`
141 - Use this to add props to the close button. For example, you can add `className`, `style` (merge and override default style), `onClick` (extend default handler), etc.
142- `toggleButtonProps: PropsObject`
143 - Use this to add props to the toggle button. For example, you can add `className`, `style` (merge and override default style), `onClick` (extend default handler), etc.
144- `position?: "top-left" | "top-right" | "bottom-left" | "bottom-right"`
145 - Defaults to `bottom-left`
146 - The position of the TanStack Router logo to open and close the devtools panel
147- `shadowDOMTarget?: ShadowRoot`
148 - Specifies a Shadow DOM target for the devtools.
149 - By default, devtool styles are applied to the `<head>` tag of the main document (light DOM). When a `shadowDOMTarget` is provided, styles will be applied within this Shadow DOM instead.
150​
151## Embedded Mode
152​
153Embedded Mode will embed the devtools as a regular component in your application. You can style it however you'd like after that!
154​
155```js
156import { TanStackRouterDevtoolsPanel } from '@tanstack/router-devtools'
157​
158function App() {
159 return (
160 <>
161 <Router router={router} />
162 <TanStackRouterDevtoolsPanel
163 router={router}
164 style={styles}
165 className={className}
166 />
167 </>
168 )
170```
171​
172### Options
173​
174Use these options to style the devtools.
175​
176- `style: StyleObject`
177 - The standard React style object used to style a component with inline styles
178- `className: string`
179 - The standard React className property used to style a component with classes