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

devtools.md

203 lines8.2 KB

1---
2id: devtools
3title: Devtools
4---
5​
6Wave your hands in the air and shout hooray because React Query comes with dedicated devtools! πŸ₯³
7​
8When you begin your React Query journey, you'll want these devtools by your side. They help visualize all the inner workings of React Query and will likely save you hours of debugging if you find yourself in a pinch!
9​
10> For Chrome, Firefox, and Edge users: Third-party browser extensions are available for debugging TanStack Query directly in browser DevTools. These provide the same functionality as the framework-specific devtools packages:
12> - <img alt="Chrome logo" src="https://www.google.com/chrome/static/images/chrome-logo.svg" width="16" height="16" class="inline mr-1 not-prose" /> <a href="https://chromewebstore.google.com/detail/tanstack-query-devtools/annajfchloimdhceglpgglpeepfghfai">Devtools for Chrome</a>
13> - <img alt="Firefox logo" src="https://upload.wikimedia.org/wikipedia/commons/a/a0/Firefox_logo%2C_2019.svg" width="16" height="16" class="inline mr-1 not-prose" /> <a href="https://addons.mozilla.org/en-US/firefox/addon/tanstack-query-devtools/">Devtools for Firefox</a>
14> - <img alt="Edge logo" src="https://upload.wikimedia.org/wikipedia/commons/9/98/Microsoft_Edge_logo_%282019%29.svg" width="16" height="16" class="inline mr-1 not-prose" /> <a href="https://microsoftedge.microsoft.com/addons/detail/tanstack-query-devtools/edmdpkgkacmjopodhfolmphdenmddobj">Devtools for Edge</a>
15​
16> For React Native users: A third-party native macOS app is available for debugging React Query in ANY js-based application. Monitor queries across devices in real-time. Check it out here: [rn-better-dev-tools](https://github.com/LovesWorking/rn-better-dev-tools)
17​
18> Note that since version 5, the dev tools support observing mutations as well.
19​
20## Install and Import the Devtools
21​
22The devtools are a separate package that you need to install:
23​
24```bash
25npm i @tanstack/react-query-devtools
26```
27​
28or
29​
30```bash
31pnpm add @tanstack/react-query-devtools
32```
33​
34or
35​
36```bash
37yarn add @tanstack/react-query-devtools
38```
39​
40or
41​
42```bash
43bun add @tanstack/react-query-devtools
44```
45​
46For Next 13+ App Dir you must install it as a dev dependency for it to work.
47​
48You can import the devtools like this:
49​
50```tsx
51import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
52```
53​
54By default, React Query Devtools are only included in bundles when `process.env.NODE_ENV === 'development'`, so you don't need to worry about excluding them during a production build.
55​
56## Floating Mode
57​
58Floating 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.
59​
60Place 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!
61​
62```tsx
63import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
64​
65function App() {
66 return (
67 <QueryClientProvider client={queryClient}>
68 {/* The rest of your application */}
69 <ReactQueryDevtools initialIsOpen={false} />
70 </QueryClientProvider>
71 )
73```
74​
75### Options
76​
77- `initialIsOpen: boolean`
78 - Set this `true` if you want the dev tools to default to being open
79- `buttonPosition?: "top-left" | "top-right" | "bottom-left" | "bottom-right" | "relative"`
80 - Defaults to `bottom-right`
81 - The position of the TanStack logo to open and close the devtools panel
82 - If `relative`, the button is placed in the location that you render the devtools.
83- `position?: "top" | "bottom" | "left" | "right"`
84 - Defaults to `bottom`
85 - The position of the React Query devtools panel
86- `client?: QueryClient`,
87 - Use this to use a custom QueryClient. Otherwise, the one from the nearest context will be used.
88- `errorTypes?: { name: string; initializer: (query: Query) => TError}[]`
89 - Use this to predefine some errors that can be triggered on your queries. Initializer will be called (with the specific query) when that error is toggled on from the UI. It must return an Error.
90- `styleNonce?: string`
91 - Use this to pass a nonce to the style tag that is added to the document head. This is useful if you are using a Content Security Policy (CSP) nonce to allow inline styles.
92- `shadowDOMTarget?: ShadowRoot`
93 - Default behavior will apply the devtool's styles to the head tag within the DOM.
94 - Use this to pass a shadow DOM target to the devtools so that the styles will be applied within the shadow DOM instead of within the head tag in the light DOM.
95- `theme?: "light" | "dark" | "system"`
96 - Defaults to `system`.
97 - Set this to change the theme of the devtools panel.
98​
99## Embedded Mode
100​
101Embedded mode will show the development tools as a fixed element in your application, so you can use our panel in your own development tools.
102​
103Place 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!
104​
105```tsx
106import { ReactQueryDevtoolsPanel } from '@tanstack/react-query-devtools'
107​
108function App() {
109 const [isOpen, setIsOpen] = React.useState(false)
110​
111 return (
112 <QueryClientProvider client={queryClient}>
113 {/* The rest of your application */}
114 <button
115 onClick={() => setIsOpen(!isOpen)}
116 >{`${isOpen ? 'Close' : 'Open'} the devtools panel`}</button>
117 {isOpen && <ReactQueryDevtoolsPanel onClose={() => setIsOpen(false)} />}
118 </QueryClientProvider>
119 )
121```
122​
123### Options
124​
125- `style?: React.CSSProperties`
126 - Custom styles for the devtools panel
127 - Default: `{ height: '500px' }`
128 - Example: `{ height: '100%' }`
129 - Example: `{ height: '100%', width: '100%' }`
130- `onClose?: () => void`
131 - Callback function that is called when the devtools panel is closed
132- `client?: QueryClient`,
133 - Use this to use a custom QueryClient. Otherwise, the one from the nearest context will be used.
134- `errorTypes?: { name: string; initializer: (query: Query) => TError}[]`
135 - Use this to predefine some errors that can be triggered on your queries. Initializer will be called (with the specific query) when that error is toggled on from the UI. It must return an Error.
136- `styleNonce?: string`
137 - Use this to pass a nonce to the style tag that is added to the document head. This is useful if you are using a Content Security Policy (CSP) nonce to allow inline styles.
138- `shadowDOMTarget?: ShadowRoot`
139 - Default behavior will apply the devtool's styles to the head tag within the DOM.
140 - Use this to pass a shadow DOM target to the devtools so that the styles will be applied within the shadow DOM instead of within the head tag in the light DOM.
141- `theme?: "light" | "dark" | "system"`
142 - Defaults to `system`.
143 - Set this to change the theme of the devtools panel.
144​
145## Devtools in production
146​
147Devtools are excluded in production builds. However, it might be desirable to lazy load the devtools in production:
148​
149```tsx
150import * as React from 'react'
151import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
152import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
153import { Example } from './Example'
154​
155const queryClient = new QueryClient()
156​
157const ReactQueryDevtoolsProduction = React.lazy(() =>
158 import('@tanstack/react-query-devtools/build/modern/production.js').then(
159 (d) => ({
160 default: d.ReactQueryDevtools,
161 }),
162 ),
164​
165function App() {
166 const [showDevtools, setShowDevtools] = React.useState(false)
167​
168 React.useEffect(() => {
169 // @ts-expect-error
170 window.toggleDevtools = () => setShowDevtools((old) => !old)
171 }, [])
172​
173 return (
174 <QueryClientProvider client={queryClient}>
175 <Example />
176 <ReactQueryDevtools initialIsOpen />
177 {showDevtools && (
178 <React.Suspense fallback={null}>
179 <ReactQueryDevtoolsProduction />
180 </React.Suspense>
181 )}
182 </QueryClientProvider>
183 )
185​
186export default App
187```
188​
189With this, calling `window.toggleDevtools()` will download the devtools bundle and show them.
190​
191### Modern bundlers
192​
193If your bundler supports package exports, you can use the following import path:
194​
195```tsx
196const ReactQueryDevtoolsProduction = React.lazy(() =>
197 import('@tanstack/react-query-devtools/production').then((d) => ({
198 default: d.ReactQueryDevtools,
199 })),
201```
202​
203For TypeScript, you would need to set `moduleResolution: 'nodenext'` in your tsconfig, which requires at least TypeScript v4.7.