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

ReactQueryDevtools.tsx

131 lines3.2 KB

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