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

devtools.md

130 lines4.4 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> 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​
26For Next 13+ App Dir you must install it as a dev dependency for it to work.
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 )
53```
54​
55### Options
56​
57- `initialIsOpen: Boolean`
58 - Set this `true` if you want the dev tools to default to being open
59- `buttonPosition?: "top-left" | "top-right" | "bottom-left" | "bottom-right"`
60 - Defaults to `bottom-right`
61 - The position of the React Query logo to open and close the devtools panel
62- `position?: "top" | "bottom" | "left" | "right"`
63 - Defaults to `bottom`
64 - The position of the React Query devtools panel
65- `client?: QueryClient`,
66 - Use this to use a custom QueryClient. Otherwise, the one from the nearest context will be used.
67- `errorTypes?: { name: string; initializer: (query: Query) => TError}`
68 - 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.
69- `styleNonce?: string`
70 - 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.
71​
72## Devtools in production
73​
74Devtools are excluded in production builds. However, it might be desirable to lazy load the devtools in production:
75​
76```tsx
77import * as React from 'react'
78import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
79import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
80import { Example } from './Example'
81​
82const queryClient = new QueryClient()
83​
84const ReactQueryDevtoolsProduction = React.lazy(() =>
85 import('@tanstack/react-query-devtools/build/modern/production.js').then(
86 (d) => ({
87 default: d.ReactQueryDevtools,
88 }),
89 ),
91​
92function App() {
93 const [showDevtools, setShowDevtools] = React.useState(false)
94​
95 React.useEffect(() => {
96 // @ts-ignore
97 window.toggleDevtools = () => setShowDevtools((old) => !old)
98 }, [])
99​
100 return (
101 <QueryClientProvider client={queryClient}>
102 <Example />
103 <ReactQueryDevtools initialIsOpen />
104 {showDevtools && (
105 <React.Suspense fallback={null}>
106 <ReactQueryDevtoolsProduction />
107 </React.Suspense>
108 )}
109 </QueryClientProvider>
110 )
112​
113export default App
114```
115​
116With this, calling `window.toggleDevtools()` will download the devtools bundle and show them.
117​
118### Modern bundlers
119​
120If your bundler supports package exports, you can use the following import path:
121​
122```tsx
123const ReactQueryDevtoolsProduction = React.lazy(() =>
124 import('@tanstack/react-query-devtools/production').then((d) => ({
125 default: d.ReactQueryDevtools,
126 })),
128```
129​
130For TypeScript, you would need to set `moduleResolution: 'nodenext'` in your tsconfig, which requires at least TypeScript v4.7.