Open History to see the last commit
1'use client'
2import * as React from 'react'
3import { onlineManager, useQueryClient } from '@tanstack/react-query'
4import { TanstackQueryDevtools } from '@tanstack/query-devtools'
5import type {
6 DevtoolsButtonPosition,
7 DevtoolsErrorType,
8 DevtoolsPosition,
9 Theme,
10} from '@tanstack/query-devtools'
11import type { QueryClient } from '@tanstack/react-query'
12β
13/**
14 * The props of `ReactQueryDevtools`, which renders the devtools with a toggle button that opens
15 * them.
16 */
17export interface DevtoolsOptions {
18 /**
19 * Set this true if you want the dev tools to default to being open
20 */
21 initialIsOpen?: boolean
22 /**
23 * The position of the TanStack logo to open and close the devtools panel.
24 * 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right' | 'relative'
25 * @defaultValue bottom-right
26 */
27 buttonPosition?: DevtoolsButtonPosition
28 /**
29 * The position of the React Query devtools panel.
30 * 'top' | 'bottom' | 'left' | 'right'
31 * @defaultValue bottom
32 */
33 position?: DevtoolsPosition
34 /**
35 * Custom instance of QueryClient
36 */
37 client?: QueryClient
38 /**
39 * Use this so you can define custom errors that can be shown in the devtools.
40 */
41 errorTypes?: Array<DevtoolsErrorType>
42 /**
43 * Use this to pass a nonce to the style tag that is added to the document head. This is useful if you are using a Content Security Policy (CSP) nonce to allow inline styles.
44 */
45 styleNonce?: string
46 /**
47 * Use this to pass a shadow DOM target to the devtools so that the styles will be applied
48 * within the shadow DOM instead of within the head tag in the light DOM.
49 */
50 shadowDOMTarget?: ShadowRoot
51 /**
52 * Set this to true to hide disabled queries from the devtools panel.
53 */
54 hideDisabledQueries?: boolean
55 /**
56 * Set this to 'light', 'dark', or 'system' to change the theme of the devtools panel.
57 * @defaultValue system
58 */
59 theme?: Theme
60}
61β
62/**
63 * Renders the TanStack Query devtools, with a toggle button that opens them, for the given or
64 * nearest `QueryClient`.
65 * @param props - The devtools options.
66 * @returns The element the devtools are mounted in.
67 */
68export function ReactQueryDevtools(
69 props: DevtoolsOptions,
70): React.ReactElement | null {
71 const queryClient = useQueryClient(props.client)
72 const ref = React.useRef<HTMLDivElement>(null)
73 const {
74 buttonPosition,
75 position,
76 initialIsOpen,
77 errorTypes,
78 styleNonce,
79 shadowDOMTarget,
80 hideDisabledQueries,
81 theme,
82 } = props
83 const [devtools] = React.useState(
84 () =>
85 new TanstackQueryDevtools({
86 client: queryClient,
87 queryFlavor: 'React Query',
88 version: '5',
89 onlineManager,
90 buttonPosition,
91 position,
92 initialIsOpen,
93 errorTypes,
94 styleNonce,
95 shadowDOMTarget,
96 hideDisabledQueries,
97 theme,
98 }),
99 )
100β
101 React.useEffect(() => {
102 devtools.setClient(queryClient)
103 }, [queryClient, devtools])
104β
105 React.useEffect(() => {
106 if (buttonPosition) {
107 devtools.setButtonPosition(buttonPosition)
108 }
109 }, [buttonPosition, devtools])
110β
111 React.useEffect(() => {
112 if (position) {
113 devtools.setPosition(position)
114 }
115 }, [position, devtools])
116β
117 React.useEffect(() => {
118 devtools.setInitialIsOpen(initialIsOpen || false)
119 }, [initialIsOpen, devtools])
120β
121 React.useEffect(() => {
122 devtools.setErrorTypes(errorTypes || [])
123 }, [errorTypes, devtools])
124β
125 React.useEffect(() => {
126 devtools.setTheme(theme)
127 }, [theme, devtools])
128β
129 React.useEffect(() => {
130 if (ref.current) {
131 devtools.mount(ref.current)
132 }
133β
134 return () => {
135 devtools.unmount()
136 }
137 }, [devtools])
138β
139 return <div dir="ltr" className="tsqd-parent-container" ref={ref}></div>
140}