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

devtools.md

220 lines9.5 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```sh
25npm i @tanstack/react-query-devtools
26```
27​
28or
29​
30```sh
31pnpm add @tanstack/react-query-devtools
32```
33​
34or
35​
36```sh
37yarn add @tanstack/react-query-devtools
38```
39​
40or
41​
42```sh
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- `hideDisabledQueries?: boolean`
96 - Defaults to `false`.
97 - Set this to true to hide disabled queries from the devtools panel.
98- `theme?: "light" | "dark" | "system"`
99 - Defaults to the theme selected in the devtools Settings menu, or `system` if none is selected.
100 - Set this to change the theme of the devtools panel. This overrides the theme selected in the Settings menu.
101​
102## Embedded Mode
103​
104Embedded mode will show the development tools as a fixed element in your application, so you can use our panel in your own development tools.
105​
106Place 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!
107​
108```tsx
109import { ReactQueryDevtoolsPanel } from '@tanstack/react-query-devtools'
110​
111function App() {
112 const [isOpen, setIsOpen] = React.useState(false)
113​
114 return (
115 <QueryClientProvider client={queryClient}>
116 {/* The rest of your application */}
117 <button
118 onClick={() => setIsOpen(!isOpen)}
119 >{`${isOpen ? 'Close' : 'Open'} the devtools panel`}</button>
120 {isOpen && <ReactQueryDevtoolsPanel onClose={() => setIsOpen(false)} />}
121 </QueryClientProvider>
122 )
124```
125​
126### Options
127​
128- `style?: React.CSSProperties`
129 - Custom styles for the devtools panel
130 - Default: `{ height: '500px' }`
131 - Example: `{ height: '100%' }`
132 - Example: `{ height: '100%', width: '100%' }`
133- `onClose?: () => void`
134 - Callback function that is called when the devtools panel is closed
135- `client?: QueryClient`,
136 - Use this to use a custom QueryClient. Otherwise, the one from the nearest context will be used.
137- `errorTypes?: { name: string; initializer: (query: Query) => TError}[]`
138 - 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.
139- `styleNonce?: string`
140 - 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.
141- `shadowDOMTarget?: ShadowRoot`
142 - Default behavior will apply the devtool's styles to the head tag within the DOM.
143 - 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.
144- `hideDisabledQueries?: boolean`
145 - Defaults to `false`.
146 - Set this to true to hide disabled queries from the devtools panel.
147- `theme?: "light" | "dark" | "system"`
148 - Defaults to the theme selected in the devtools Settings menu, or `system` if none is selected.
149 - Set this to change the theme of the devtools panel. This overrides the theme selected in the Settings menu.
150​
151## Search query and mutation data
152​
153Select a query or mutation and use **Search data** in its Data Explorer to search nested property names and primitive values. Search uses case-insensitive text matching and treats special characters as plain text.
154​
155Matching text is highlighted. Matching editable values have a highlighted input. The explorer opens matching branches and the first matching page in each paginated collection. Other pages with matches are highlighted so you can open them. You can still inspect other rows and edit query data.
156​
157The Data Explorer highlights up to 1,000 matches at a time.
158​
159Select **Clear** or press **Escape** in the search field to restore your manual expansion. Selecting another query or mutation clears the search.
160​
161## Devtools in production
162​
163Devtools are excluded in production builds. However, it might be desirable to lazy load the devtools in production:
164​
165```tsx
166import * as React from 'react'
167import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
168import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
169import { Example } from './Example'
170​
171const queryClient = new QueryClient()
172​
173const ReactQueryDevtoolsProduction = React.lazy(() =>
174 import('@tanstack/react-query-devtools/build/modern/production.js').then(
175 (d) => ({
176 default: d.ReactQueryDevtools,
177 }),
178 ),
180​
181function App() {
182 const [showDevtools, setShowDevtools] = React.useState(false)
183​
184 React.useEffect(() => {
185 Object.assign(window, {
186 toggleDevtools: () => setShowDevtools((old) => !old),
187 })
188 }, [])
189​
190 return (
191 <QueryClientProvider client={queryClient}>
192 <Example />
193 <ReactQueryDevtools initialIsOpen />
194 {showDevtools && (
195 <React.Suspense fallback={null}>
196 <ReactQueryDevtoolsProduction />
197 </React.Suspense>
198 )}
199 </QueryClientProvider>
200 )
202​
203export default App
204```
205​
206With this, calling `window.toggleDevtools()` will download the devtools bundle and show them.
207​
208### Modern bundlers
209​
210If your bundler supports package exports, you can use the following import path:
211​
212```tsx
213const ReactQueryDevtoolsProduction = React.lazy(() =>
214 import('@tanstack/react-query-devtools/production').then((d) => ({
215 default: d.ReactQueryDevtools,
216 })),
218```
219​
220For TypeScript, you would need to set `moduleResolution: 'nodenext'` in your tsconfig, which requires at least TypeScript v4.7.