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

devtools.md

158 lines5.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## Install and Import the Devtools
13​
14The devtools are a separate package that you need to install:
15​
16```bash
17$ npm i @tanstack/react-query-devtools
18# or
19$ pnpm add @tanstack/react-query-devtools
20# or
21$ yarn add @tanstack/react-query-devtools
22```
23​
24You can import the devtools like this:
25​
26```tsx
27import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
28```
29​
30By 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.
31​
32## Floating Mode
33​
34Floating 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.
35​
36Place 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!
37​
38```tsx
39import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
40​
41function App() {
42 return (
43 <QueryClientProvider client={queryClient}>
44 {/* The rest of your application */}
45 <ReactQueryDevtools initialIsOpen={false} />
46 </QueryClientProvider>
47 )
49```
50​
51### Options
52​
53- `initialIsOpen: Boolean`
54 - Set this `true` if you want the dev tools to default to being open
55- `panelProps: PropsObject`
56 - Use this to add props to the panel. For example, you can add `className`, `style` (merge and override default style), etc.
57- `closeButtonProps: PropsObject`
58 - 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.
59- `toggleButtonProps: PropsObject`
60 - 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.
61- `position?: "top-left" | "top-right" | "bottom-left" | "bottom-right"`
62 - Defaults to `bottom-left`
63 - The position of the React Query logo to open and close the devtools panel
64- `panelPosition?: "top" | "bottom" | "left" | "right"`
65 - Defaults to `bottom`
66 - The position of the React Query devtools panel
67- `context?: React.Context<QueryClient | undefined>`
68 - Use this to use a custom React Query context. Otherwise, `defaultContext` will be used.
69​
70## Embedded Mode
71​
72Embedded Mode will embed the devtools as a regular component in your application. You can style it however you'd like after that!
73​
74```tsx
75import { ReactQueryDevtoolsPanel } from '@tanstack/react-query-devtools'
76​
77function App() {
78 return (
79 <QueryClientProvider client={queryClient}>
80 {/* The rest of your application */}
81 <ReactQueryDevtoolsPanel style={styles} className={className} />
82 </QueryClientProvider>
83 )
85```
86​
87### Options
88​
89Use these options to style the dev tools.
90​
91- `style: StyleObject`
92 - The standard React style object used to style a component with inline styles
93- `className: string`
94 - The standard React className property used to style a component with classes
95- `showCloseButton?: boolean`
96 - Show a close button inside the devtools panel
97- `closeButtonProps: PropsObject`
98 - 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.
99​
100## Devtools in production
101​
102Devtools are excluded in production builds. However, it might be desirable to lazy load the devtools in production:
103​
104```tsx
105import * as React from 'react'
106import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
107import { ReactQueryDevtools } from '@tanstack/react-query-devtools'
108import { Example } from './Example'
109​
110const queryClient = new QueryClient()
111​
112const ReactQueryDevtoolsProduction = React.lazy(() =>
113 import('@tanstack/react-query-devtools/build/lib/index.prod.js').then(
114 (d) => ({
115 default: d.ReactQueryDevtools,
116 }),
117 ),
119​
120function App() {
121 const [showDevtools, setShowDevtools] = React.useState(false)
122​
123 React.useEffect(() => {
124 // @ts-ignore
125 window.toggleDevtools = () => setShowDevtools((old) => !old)
126 }, [])
127​
128 return (
129 <QueryClientProvider client={queryClient}>
130 <Example />
131 <ReactQueryDevtools initialIsOpen />
132 {showDevtools && (
133 <React.Suspense fallback={null}>
134 <ReactQueryDevtoolsProduction />
135 </React.Suspense>
136 )}
137 </QueryClientProvider>
138 )
140​
141export default App
142```
143​
144With this, calling `window.toggleDevtools()` will download the devtools bundle and show them.
145​
146### Modern bundlers
147​
148If your bundler supports package exports, you can use the following import path:
149​
150```tsx
151const ReactQueryDevtoolsProduction = React.lazy(() =>
152 import('@tanstack/react-query-devtools/production').then((d) => ({
153 default: d.ReactQueryDevtools,
154 })),
156```
157​
158For TypeScript, you would need to set `moduleResolution: 'nodenext'` in your tsconfig, which requires at least TypeScript v4.7.