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

devtools.md

135 lines4.8 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 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> However, an external tool is available that enables the use of React Query DevTools with React Native projects. Find out more and contribute on [GitHub](https://github.com/LovesWorking/react-query-external-sync)..
13​
14> Also note that you can use these devtools to observe queries, but **not mutations**
15​
16## Install and Import the Devtools
17​
18The devtools are a separate package that you need to install:
19​
20```bash
21$ npm i @tanstack/react-query-devtools
22# or
23$ pnpm add @tanstack/react-query-devtools
24# or
25$ yarn add @tanstack/react-query-devtools
26# or
27$ bun add @tanstack/react-query-devtools
28```
29​
30For Next 13+ App Dir you must install it as a dev dependency for it to work.
31​
32You can import the devtools like this:
33​
34```tsx
35import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
36```
37​
38By 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.
39​
40## Floating Mode
41​
42Floating 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.
43​
44Place 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!
45​
46```tsx
47import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
48​
49function App() {
50 return (
51 <QueryClientProvider client={queryClient}>
52 {/* The rest of your application */}
53 <ReactQueryDevtools initialIsOpen={false} />
54 </QueryClientProvider>
55 )
57```
58​
59### Options
60​
61- `initialIsOpen: Boolean`
62 - Set this `true` if you want the dev tools to default to being open
63- `buttonPosition?: "top-left" | "top-right" | "bottom-left" | "bottom-right" | "relative"`
64 - Defaults to `bottom-right`
65 - The position of the React Query logo to open and close the devtools panel
66 - If `relative`, the button is placed in the location that you render the devtools.
67- `position?: "top" | "bottom" | "left" | "right"`
68 - Defaults to `bottom`
69 - The position of the React Query devtools panel
70- `client?: QueryClient`,
71 - Use this to use a custom QueryClient. Otherwise, the one from the nearest context will be used.
72- `errorTypes?: { name: string; initializer: (query: Query) => TError}`
73 - 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.
74- `styleNonce?: string`
75 - 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.
76​
77## Devtools in production
78​
79Devtools are excluded in production builds. However, it might be desirable to lazy load the devtools in production:
80​
81```tsx
82import * as React from 'react'
83import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
84import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
85import { Example } from './Example'
86​
87const queryClient = new QueryClient()
88​
89const ReactQueryDevtoolsProduction = React.lazy(() =>
90 import('@tanstack/react-query-devtools/build/modern/production.js').then(
91 (d) => ({
92 default: d.ReactQueryDevtools,
93 }),
94 ),
96​
97function App() {
98 const [showDevtools, setShowDevtools] = React.useState(false)
99​
100 React.useEffect(() => {
101 // @ts-expect-error
102 window.toggleDevtools = () => setShowDevtools((old) => !old)
103 }, [])
104​
105 return (
106 <QueryClientProvider client={queryClient}>
107 <Example />
108 <ReactQueryDevtools initialIsOpen />
109 {showDevtools && (
110 <React.Suspense fallback={null}>
111 <ReactQueryDevtoolsProduction />
112 </React.Suspense>
113 )}
114 </QueryClientProvider>
115 )
117​
118export default App
119```
120​
121With this, calling `window.toggleDevtools()` will download the devtools bundle and show them.
122​
123### Modern bundlers
124​
125If your bundler supports package exports, you can use the following import path:
126​
127```tsx
128const ReactQueryDevtoolsProduction = React.lazy(() =>
129 import('@tanstack/react-query-devtools/production').then((d) => ({
130 default: d.ReactQueryDevtools,
131 })),
133```
134​
135For TypeScript, you would need to set `moduleResolution: 'nodenext'` in your tsconfig, which requires at least TypeScript v4.7.