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

devtools.md

233 lines10.8 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 - Deprecated. The devtools no longer add a `<style>` element, so they need no nonce. This option does nothing.
92- `shadowDOMTarget?: ShadowRoot`
93 - The shadow root to add the devtools styles to. By default, the styles are added to the shadow root the devtools render in, or to the document when they do not render in a shadow root.
94- `trustedTypesPolicy?: { createHTML: (html: string) => unknown }`
95 - The Trusted Types policy the devtools use to create their HTML templates. Pass one when your page enforces `require-trusted-types-for 'script'`. The devtools only pass it their own markup, so a policy that returns its input is enough, for example `trustedTypes.createPolicy('devtools', { createHTML: (html) => html })`.
96- `hideDisabledQueries?: boolean`
97 - Defaults to `false`.
98 - Set this to true to hide disabled queries from the devtools panel.
99- `theme?: "light" | "dark" | "system"`
100 - Defaults to the theme selected in the devtools Settings menu, or `system` if none is selected.
101 - Set this to change the theme of the devtools panel. This overrides the theme selected in the Settings menu.
102​
103## Embedded Mode
104​
105Embedded mode will show the development tools as a fixed element in your application, so you can use our panel in your own development tools.
106​
107Place 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!
108​
109```tsx
110import { ReactQueryDevtoolsPanel } from '@tanstack/react-query-devtools'
111​
112function App() {
113 const [isOpen, setIsOpen] = React.useState(false)
114​
115 return (
116 <QueryClientProvider client={queryClient}>
117 {/* The rest of your application */}
118 <button
119 onClick={() => setIsOpen(!isOpen)}
120 >{`${isOpen ? 'Close' : 'Open'} the devtools panel`}</button>
121 {isOpen && <ReactQueryDevtoolsPanel onClose={() => setIsOpen(false)} />}
122 </QueryClientProvider>
123 )
125```
126​
127### Options
128​
129- `style?: React.CSSProperties`
130 - Custom styles for the devtools panel
131 - Default: `{ height: '100%', display: 'flex', flexDirection: 'column' }`
132 - The panel fills the height of its container, or is `500px` tall when the container has no set height.
133 - Example: `{ height: '100%' }`
134 - Example: `{ height: '100%', width: '100%' }`
135- `onClose?: () => void`
136 - Callback function that is called when the devtools panel is closed
137- `client?: QueryClient`,
138 - Use this to use a custom QueryClient. Otherwise, the one from the nearest context will be used.
139- `errorTypes?: { name: string; initializer: (query: Query) => TError}[]`
140 - 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.
141- `styleNonce?: string`
142 - Deprecated. The devtools no longer add a `<style>` element, so they need no nonce. This option does nothing.
143- `shadowDOMTarget?: ShadowRoot`
144 - The shadow root to add the devtools styles to. By default, the styles are added to the shadow root the devtools render in, or to the document when they do not render in a shadow root.
145- `trustedTypesPolicy?: { createHTML: (html: string) => unknown }`
146 - The Trusted Types policy the devtools use to create their HTML templates. Pass one when your page enforces `require-trusted-types-for 'script'`. The devtools only pass it their own markup, so a policy that returns its input is enough, for example `trustedTypes.createPolicy('devtools', { createHTML: (html) => html })`.
147- `hideDisabledQueries?: boolean`
148 - Defaults to `false`.
149 - Set this to true to hide disabled queries from the devtools panel.
150- `theme?: "light" | "dark" | "system"`
151 - Defaults to the theme selected in the devtools Settings menu, or `system` if none is selected.
152 - Set this to change the theme of the devtools panel. This overrides the theme selected in the Settings menu.
153​
154## Filter queries and mutations
155​
156The **Filter** input above the query and mutation lists uses the **Key** scope by default. Select the scope button inside the input to switch between **Key**, **Data**, and **All**:
157​
158- **Key** matches the query hash, or the mutation key and submission time.
159- **Data** matches the query data, or the mutation variables or data, the same way **Search data** does below.
160- **All** matches either.
161​
162The selected scope is remembered across reloads, like the other filter settings.
163​
164## Search query and mutation data
165​
166Select 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.
167​
168Matching 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.
169​
170The Data Explorer highlights up to 1,000 matches at a time.
171​
172Select **Clear** or press **Escape** in the search field to restore your manual expansion. Selecting another query or mutation clears the search, unless the filter scope is **Data** or **All**: then the search starts with the filter text. For a mutation, it starts only in the variables or data that match the filter.
173​
174## Devtools in production
175​
176Devtools are excluded in production builds. However, it might be desirable to lazy load the devtools in production:
177​
178```tsx
179import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
180import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
181import * as React from 'react'
182import { Example } from './Example'
183​
184const queryClient = new QueryClient()
185​
186const ReactQueryDevtoolsProduction = React.lazy(() =>
187 import('@tanstack/react-query-devtools/build/modern/production.js').then(
188 (d) => ({
189 default: d.ReactQueryDevtools,
190 }),
191 ),
193​
194function App() {
195 const [showDevtools, setShowDevtools] = React.useState(false)
196​
197 React.useEffect(() => {
198 Object.assign(window, {
199 toggleDevtools: () => setShowDevtools((old) => !old),
200 })
201 }, [])
202​
203 return (
204 <QueryClientProvider client={queryClient}>
205 <Example />
206 <ReactQueryDevtools initialIsOpen />
207 {showDevtools && (
208 <React.Suspense fallback={null}>
209 <ReactQueryDevtoolsProduction />
210 </React.Suspense>
211 )}
212 </QueryClientProvider>
213 )
215​
216export default App
217```
218​
219With this, calling `window.toggleDevtools()` will download the devtools bundle and show them.
220​
221### Modern bundlers
222​
223If your bundler supports package exports, you can use the following import path:
224​
225```tsx
226const ReactQueryDevtoolsProduction = React.lazy(() =>
227 import('@tanstack/react-query-devtools/production').then((d) => ({
228 default: d.ReactQueryDevtools,
229 })),
231```
232​
233For TypeScript, you would need to set `moduleResolution: 'nodenext'` in your tsconfig, which requires at least TypeScript v4.7.