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

devtools.md

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