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

devtools.md

191 lines6.9 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 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 )
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 )
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 ),
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 )
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 })),
189```
190​
191For TypeScript, you would need to set `moduleResolution: 'nodenext'` in your tsconfig, which requires at least TypeScript v4.7.