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

devtools.md

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