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

devtools.md

160 lines5.5 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​
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 )
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​
72## Embedded Mode
73​
74Embedded Mode will embed the devtools as a regular component in your application. You can style it however you'd like after that!
75​
76```tsx
77import { ReactQueryDevtoolsPanel } from '@tanstack/react-query-devtools'
78​
79function App() {
80 return (
81 <QueryClientProvider client={queryClient}>
82 {/* The rest of your application */}
83 <ReactQueryDevtoolsPanel style={styles} className={className} />
84 </QueryClientProvider>
85 )
87```
88​
89### Options
90​
91Use these options to style the dev tools.
92​
93- `style: StyleObject`
94 - The standard React style object used to style a component with inline styles
95- `className: string`
96 - The standard React className property used to style a component with classes
97- `showCloseButton?: boolean`
98 - Show a close button inside the devtools panel
99- `closeButtonProps: PropsObject`
100 - 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.
101​
102## Devtools in production
103​
104Devtools are excluded in production builds. However, it might be desirable to lazy load the devtools in production:
105​
106```tsx
107import * as React from 'react'
108import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
109import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
110import { Example } from './Example'
111​
112const queryClient = new QueryClient()
113​
114const ReactQueryDevtoolsProduction = React.lazy(() =>
115 import('@tanstack/react-query-devtools/build/lib/index.prod.js').then(
116 (d) => ({
117 default: d.ReactQueryDevtools,
118 }),
119 ),
121​
122function App() {
123 const [showDevtools, setShowDevtools] = React.useState(false)
124​
125 React.useEffect(() => {
126 // @ts-ignore
127 window.toggleDevtools = () => setShowDevtools((old) => !old)
128 }, [])
129​
130 return (
131 <QueryClientProvider client={queryClient}>
132 <Example />
133 <ReactQueryDevtools initialIsOpen />
134 {showDevtools && (
135 <React.Suspense fallback={null}>
136 <ReactQueryDevtoolsProduction />
137 </React.Suspense>
138 )}
139 </QueryClientProvider>
140 )
142​
143export default App
144```
145​
146With this, calling `window.toggleDevtools()` will download the devtools bundle and show them.
147​
148### Modern bundlers
149​
150If your bundler supports package exports, you can use the following import path:
151​
152```tsx
153const ReactQueryDevtoolsProduction = React.lazy(() =>
154 import('@tanstack/react-query-devtools/production').then((d) => ({
155 default: d.ReactQueryDevtools,
156 })),
158```
159​
160For TypeScript, you would need to set `moduleResolution: 'nodenext'` in your tsconfig, which requires at least TypeScript v4.7.