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

devtools.md

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