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 of 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> Also note that you can use these devtools to observe queries, but **not mutations**
13β
14## Install and Import the Devtools
15β
16The devtools are a separate package that you need to install.
17β
18The major version of all the packages must be in sync.
19β
20```bash
21$ npm i @tanstack/react-query-devtools@4
22# or
23$ pnpm add @tanstack/react-query-devtools@4
24# or
25$ yarn add @tanstack/react-query-devtools@4
26```
27β
28You can import the devtools like this:
29β
30```tsx
31import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
32```
33β
34By 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.
35β
36## Floating Mode
37β
38Floating 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.
39β
40Place 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!
41β
42```tsx
43import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
44β
45function App() {
46 return (
47 <QueryClientProvider client={queryClient}>
48 {/* The rest of your application */}
49 <ReactQueryDevtools initialIsOpen={false} />
50 </QueryClientProvider>
51 )
52}
53```
54β
55### Options
56β
57- `initialIsOpen: Boolean`
58 - Set this `true` if you want the dev tools to default to being open
59- `panelProps: PropsObject`
60 - Use this to add props to the panel. For example, you can add `className`, `style` (merge and override default style), etc.
61- `closeButtonProps: PropsObject`
62 - Use this to add props to the close button. For example, you can add `className`, `style` (merge and override default style), `onClick` (extend default handler), etc.
63- `toggleButtonProps: PropsObject`
64 - Use this to add props to the toggle button. For example, you can add `className`, `style` (merge and override default style), `onClick` (extend default handler), etc.
65- `position?: "top-left" | "top-right" | "bottom-left" | "bottom-right"`
66 - Defaults to `bottom-left`
67 - The position of the React Query logo to open and close the devtools panel
68- `panelPosition?: "top" | "bottom" | "left" | "right"`
69 - Defaults to `bottom`
70 - The position of the React Query devtools panel
71- `context?: React.Context<QueryClient | undefined>`
72 - Use this to use a custom React Query context. Otherwise, `defaultContext` will be used.
73- `errorTypes?: { name: string; initializer: (query: Query) => { toString(): string }}`
74 - 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 item that can be stringified so we can check for its presence on any given query.
75β
76## Embedded Mode
77β
78Embedded Mode will embed the devtools as a regular component in your application. You can style it however you'd like after that!
79β
80```tsx
81import { ReactQueryDevtoolsPanel } from '@tanstack/react-query-devtools'
82β
83function App() {
84 return (
85 <QueryClientProvider client={queryClient}>
86 {/* The rest of your application */}
87 <ReactQueryDevtoolsPanel style={styles} className={className} />
88 </QueryClientProvider>
89 )
90}
91```
92β
93### Options
94β
95Use these options to style the dev tools.
96β
97- `style: StyleObject`
98 - The standard React style object used to style a component with inline styles
99- `className: string`
100 - The standard React className property used to style a component with classes
101- `showCloseButton?: boolean`
102 - Show a close button inside the devtools panel
103- `closeButtonProps: PropsObject`
104 - Use this to add props to the close button. For example, you can add `className`, `style` (merge and override default style), `onClick` (extend default handler), etc.
105β
106## Devtools in production
107β
108Devtools are excluded in production builds. However, it might be desirable to lazy load the devtools in production:
109β
110```tsx
111import * as React from 'react'
112import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
113import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
114import { Example } from './Example'
115β
116const queryClient = new QueryClient()
117β
118const ReactQueryDevtoolsProduction = React.lazy(() =>
119 import('@tanstack/react-query-devtools/build/lib/index.prod.js').then(
120 (d) => ({
121 default: d.ReactQueryDevtools,
122 }),
123 ),
124)
125β
126function App() {
127 const [showDevtools, setShowDevtools] = React.useState(false)
128β
129 React.useEffect(() => {
130 // @ts-ignore
131 window.toggleDevtools = () => setShowDevtools((old) => !old)
132 }, [])
133β
134 return (
135 <QueryClientProvider client={queryClient}>
136 <Example />
137 <ReactQueryDevtools initialIsOpen />
138 {showDevtools && (
139 <React.Suspense fallback={null}>
140 <ReactQueryDevtoolsProduction />
141 </React.Suspense>
142 )}
143 </QueryClientProvider>
144 )
145}
146β
147export default App
148```
149β
150With this, calling `window.toggleDevtools()` will download the devtools bundle and show them.
151β
152### Modern bundlers
153β
154If your bundler supports package exports, you can use the following import path:
155β
156```tsx
157const ReactQueryDevtoolsProduction = React.lazy(() =>
158 import('@tanstack/react-query-devtools/production').then((d) => ({
159 default: d.ReactQueryDevtools,
160 })),
161)
162```
163β
164For TypeScript, you would need to set `moduleResolution: 'nodenext'` in your tsconfig, which requires at least TypeScript v4.7.