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β
13export interface DevtoolsOptions {
14 /**
15 * Set this true if you want the dev tools to default to being open
16 */
17 initialIsOpen?: boolean
18 /**
19 * The position of the React Query logo to open and close the devtools panel.
20 * 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right'
21 * Defaults to 'bottom-right'.
22 */
23 buttonPosition?: DevtoolsButtonPosition
24 /**
25 * The position of the React Query devtools panel.
26 * 'top' | 'bottom' | 'left' | 'right'
27 * Defaults to 'bottom'.
28 */
29 position?: DevtoolsPosition
30 /**
31 * Custom instance of QueryClient
32 */
33 client?: QueryClient
34 /**
35 * Use this so you can define custom errors that can be shown in the devtools.
36 */
37 errorTypes?: Array<DevtoolsErrorType>
38 /**
39 * 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.
40 */
41 styleNonce?: string
42 /**
43 * Use this so you can attach the devtool's styles to specific element in the DOM.
44 */
45 shadowDOMTarget?: ShadowRoot
46 /**
47 * Set this to true to hide disabled queries from the devtools panel.
48 */
49 hideDisabledQueries?: boolean
50 /**
51 * Set this to 'light', 'dark', or 'system' to change the theme of the devtools panel.
52 * Defaults to 'system'.
53 */
54 theme?: Theme
55}
56β
57export function ReactQueryDevtools(
58 props: DevtoolsOptions,
59): React.ReactElement | null {
60 const queryClient = useQueryClient(props.client)
61 const ref = React.useRef<HTMLDivElement>(null)
62 const {
63 buttonPosition,
64 position,
65 initialIsOpen,
66 errorTypes,
67 styleNonce,
68 shadowDOMTarget,
69 hideDisabledQueries,
70 theme,
71 } = props
72 const [devtools] = React.useState(
73 new TanstackQueryDevtools({
74 client: queryClient,
75 queryFlavor: 'React Query',
76 version: '5',
77 onlineManager,
78 buttonPosition,
79 position,
80 initialIsOpen,
81 errorTypes,
82 styleNonce,
83 shadowDOMTarget,
84 hideDisabledQueries,
85 theme,
86 }),
87 )
88β
89 React.useEffect(() => {
90 devtools.setClient(queryClient)
91 }, [queryClient, devtools])
92β
93 React.useEffect(() => {
94 if (buttonPosition) {
95 devtools.setButtonPosition(buttonPosition)
96 }
97 }, [buttonPosition, devtools])
98β
99 React.useEffect(() => {
100 if (position) {
101 devtools.setPosition(position)
102 }
103 }, [position, devtools])
104β
105 React.useEffect(() => {
106 devtools.setInitialIsOpen(initialIsOpen || false)
107 }, [initialIsOpen, devtools])
108β
109 React.useEffect(() => {
110 devtools.setErrorTypes(errorTypes || [])
111 }, [errorTypes, devtools])
112β
113 React.useEffect(() => {
114 devtools.setTheme(theme)
115 }, [theme, devtools])
116β
117 React.useEffect(() => {
118 if (ref.current) {
119 devtools.mount(ref.current)
120 }
121β
122 return () => {
123 devtools.unmount()
124 }
125 }, [devtools])
126β
127 return <div dir="ltr" className="tsqd-parent-container" ref={ref}></div>
128}