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