Open History to see the last commit
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 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)
11β
12> Note that since version 5, the dev tools support observing mutations as well.
13β
14## Install and Import the Devtools
15β
16The devtools are a separate package that you need to install:
17β
18```bash
19npm i @tanstack/react-query-devtools
20```
21β
22or
23β
24```bash
25pnpm add @tanstack/react-query-devtools
26```
27β
28or
29β
30```bash
31yarn add @tanstack/react-query-devtools
32```
33β
34or
35β
36```bash
37bun add @tanstack/react-query-devtools
38```
39β
40For Next 13+ App Dir you must install it as a dev dependency for it to work.
41β
42You can import the devtools like this:
43β
44```tsx
45import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
46```
47β
48By 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.
49β
50## Floating Mode
51β
52Floating 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.
53β
54Place 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!
55β
56```tsx
57import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
58β
59function App() {
60 return (
61 <QueryClientProvider client={queryClient}>
62 {/* The rest of your application */}
63 <ReactQueryDevtools initialIsOpen={false} />
64 </QueryClientProvider>
65 )
66}
67```
68β
69### Options
70β
71- `initialIsOpen: Boolean`
72 - Set this `true` if you want the dev tools to default to being open
73- `buttonPosition?: "top-left" | "top-right" | "bottom-left" | "bottom-right" | "relative"`
74 - Defaults to `bottom-right`
75 - The position of the React Query logo to open and close the devtools panel
76 - If `relative`, the button is placed in the location that you render the devtools.
77- `position?: "top" | "bottom" | "left" | "right"`
78 - Defaults to `bottom`
79 - The position of the React Query devtools panel
80- `client?: QueryClient`,
81 - Use this to use a custom QueryClient. Otherwise, the one from the nearest context will be used.
82- `errorTypes?: { name: string; initializer: (query: Query) => TError}[]`
83 - 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.
84- `styleNonce?: string`
85 - 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.
86- `shadowDOMTarget?: ShadowRoot`
87 - Default behavior will apply the devtool's styles to the head tag within the DOM.
88 - 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.
89β
90## Embedded Mode
91β
92Embedded mode will show the development tools as a fixed element in your application, so you can use our panel in your own development tools.
93β
94Place 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!
95β
96```tsx
97import { ReactQueryDevtoolsPanel } from '@tanstack/react-query-devtools'
98β
99function App() {
100 const [isOpen, setIsOpen] = React.useState(false)
101β
102 return (
103 <QueryClientProvider client={queryClient}>
104 {/* The rest of your application */}
105 <button
106 onClick={() => setIsOpen(!isOpen)}
107 >{`${isOpen ? 'Close' : 'Open'} the devtools panel`}</button>
108 {isOpen && <ReactQueryDevtoolsPanel onClose={() => setIsOpen(false)} />}
109 </QueryClientProvider>
110 )
111}
112```
113β
114### Options
115β
116- `style?: React.CSSProperties`
117 - Custom styles for the devtools panel
118 - Default: `{ height: '500px' }`
119 - Example: `{ height: '100%' }`
120 - Example: `{ height: '100%', width: '100%' }`
121- `onClose?: () => unknown`
122 - Callback function that is called when the devtools panel is closed
123- `client?: QueryClient`,
124 - Use this to use a custom QueryClient. Otherwise, the one from the nearest context will be used.
125- `errorTypes?: { name: string; initializer: (query: Query) => TError}[]`
126 - 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.
127- `styleNonce?: string`
128 - 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.
129- `shadowDOMTarget?: ShadowRoot`
130 - Default behavior will apply the devtool's styles to the head tag within the DOM.
131 - 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.
132β
133## Devtools in production
134β
135Devtools are excluded in production builds. However, it might be desirable to lazy load the devtools in production:
136β
137```tsx
138import * as React from 'react'
139import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
140import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
141import { Example } from './Example'
142β
143const queryClient = new QueryClient()
144β
145const ReactQueryDevtoolsProduction = React.lazy(() =>
146 import('@tanstack/react-query-devtools/build/modern/production.js').then(
147 (d) => ({
148 default: d.ReactQueryDevtools,
149 }),
150 ),
151)
152β
153function App() {
154 const [showDevtools, setShowDevtools] = React.useState(false)
155β
156 React.useEffect(() => {
157 // @ts-expect-error
158 window.toggleDevtools = () => setShowDevtools((old) => !old)
159 }, [])
160β
161 return (
162 <QueryClientProvider client={queryClient}>
163 <Example />
164 <ReactQueryDevtools initialIsOpen />
165 {showDevtools && (
166 <React.Suspense fallback={null}>
167 <ReactQueryDevtoolsProduction />
168 </React.Suspense>
169 )}
170 </QueryClientProvider>
171 )
172}
173β
174export default App
175```
176β
177With this, calling `window.toggleDevtools()` will download the devtools bundle and show them.
178β
179### Modern bundlers
180β
181If your bundler supports package exports, you can use the following import path:
182β
183```tsx
184const ReactQueryDevtoolsProduction = React.lazy(() =>
185 import('@tanstack/react-query-devtools/production').then((d) => ({
186 default: d.ReactQueryDevtools,
187 })),
188)
189```
190β
191For TypeScript, you would need to set `moduleResolution: 'nodenext'` in your tsconfig, which requires at least TypeScript v4.7.