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> 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 )
70}
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## Devtools in production
95β
96Devtools are excluded in production builds. However, it might be desirable to lazy load the devtools in production:
97β
98```tsx
99import * as React from 'react'
100import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
101import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
102import { Example } from './Example'
103β
104const queryClient = new QueryClient()
105β
106const ReactQueryDevtoolsProduction = React.lazy(() =>
107 import('@tanstack/react-query-devtools/build/modern/production.js').then(
108 (d) => ({
109 default: d.ReactQueryDevtools,
110 }),
111 ),
112)
113β
114function App() {
115 const [showDevtools, setShowDevtools] = React.useState(false)
116β
117 React.useEffect(() => {
118 // @ts-expect-error
119 window.toggleDevtools = () => setShowDevtools((old) => !old)
120 }, [])
121β
122 return (
123 <QueryClientProvider client={queryClient}>
124 <Example />
125 <ReactQueryDevtools initialIsOpen />
126 {showDevtools && (
127 <React.Suspense fallback={null}>
128 <ReactQueryDevtoolsProduction />
129 </React.Suspense>
130 )}
131 </QueryClientProvider>
132 )
133}
134β
135export default App
136```
137β
138With this, calling `window.toggleDevtools()` will download the devtools bundle and show them.
139β
140### Modern bundlers
141β
142If your bundler supports package exports, you can use the following import path:
143β
144```tsx
145const ReactQueryDevtoolsProduction = React.lazy(() =>
146 import('@tanstack/react-query-devtools/production').then((d) => ({
147 default: d.ReactQueryDevtools,
148 })),
149)
150```
151β
152For TypeScript, you would need to set `moduleResolution: 'nodenext'` in your tsconfig, which requires at least TypeScript v4.7.