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

devtools.md

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