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