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

devtools.md

197 lines8.0 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" /> [Devtools for Chrome](https://chromewebstore.google.com/detail/tanstack-query-devtools/annajfchloimdhceglpgglpeepfghfai)
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" /> [Devtools for Firefox](https://addons.mozilla.org/en-US/firefox/addon/tanstack-query-devtools/)
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" /> [Devtools for Edge](https://microsoftedge.microsoft.com/addons/detail/tanstack-query-devtools/edmdpkgkacmjopodhfolmphdenmddobj)
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 React Query 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​
96## Embedded Mode
97​
98Embedded mode will show the development tools as a fixed element in your application, so you can use our panel in your own development tools.
99​
100Place 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!
101​
102```tsx
103import { ReactQueryDevtoolsPanel } from '@tanstack/react-query-devtools'
104​
105function App() {
106 const [isOpen, setIsOpen] = React.useState(false)
107​
108 return (
109 <QueryClientProvider client={queryClient}>
110 {/* The rest of your application */}
111 <button
112 onClick={() => setIsOpen(!isOpen)}
113 >{`${isOpen ? 'Close' : 'Open'} the devtools panel`}</button>
114 {isOpen && <ReactQueryDevtoolsPanel onClose={() => setIsOpen(false)} />}
115 </QueryClientProvider>
116 )
118```
119​
120### Options
121​
122- `style?: React.CSSProperties`
123 - Custom styles for the devtools panel
124 - Default: `{ height: '500px' }`
125 - Example: `{ height: '100%' }`
126 - Example: `{ height: '100%', width: '100%' }`
127- `onClose?: () => unknown`
128 - Callback function that is called when the devtools panel is closed
129- `client?: QueryClient`,
130 - Use this to use a custom QueryClient. Otherwise, the one from the nearest context will be used.
131- `errorTypes?: { name: string; initializer: (query: Query) => TError}[]`
132 - 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.
133- `styleNonce?: string`
134 - 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.
135- `shadowDOMTarget?: ShadowRoot`
136 - Default behavior will apply the devtool's styles to the head tag within the DOM.
137 - 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.
138​
139## Devtools in production
140​
141Devtools are excluded in production builds. However, it might be desirable to lazy load the devtools in production:
142​
143```tsx
144import * as React from 'react'
145import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
146import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
147import { Example } from './Example'
148​
149const queryClient = new QueryClient()
150​
151const ReactQueryDevtoolsProduction = React.lazy(() =>
152 import('@tanstack/react-query-devtools/build/modern/production.js').then(
153 (d) => ({
154 default: d.ReactQueryDevtools,
155 }),
156 ),
158​
159function App() {
160 const [showDevtools, setShowDevtools] = React.useState(false)
161​
162 React.useEffect(() => {
163 // @ts-expect-error
164 window.toggleDevtools = () => setShowDevtools((old) => !old)
165 }, [])
166​
167 return (
168 <QueryClientProvider client={queryClient}>
169 <Example />
170 <ReactQueryDevtools initialIsOpen />
171 {showDevtools && (
172 <React.Suspense fallback={null}>
173 <ReactQueryDevtoolsProduction />
174 </React.Suspense>
175 )}
176 </QueryClientProvider>
177 )
179​
180export default App
181```
182​
183With this, calling `window.toggleDevtools()` will download the devtools bundle and show them.
184​
185### Modern bundlers
186​
187If your bundler supports package exports, you can use the following import path:
188​
189```tsx
190const ReactQueryDevtoolsProduction = React.lazy(() =>
191 import('@tanstack/react-query-devtools/production').then((d) => ({
192 default: d.ReactQueryDevtools,
193 })),
195```
196​
197For TypeScript, you would need to set `moduleResolution: 'nodenext'` in your tsconfig, which requires at least TypeScript v4.7.