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

ReactQueryDevtools.tsx

116 lines3.0 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} 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 * Set this to true to hide disabled queries from the devtools panel.
47 */
48 hideDisabledQueries?: boolean
50​
51export function ReactQueryDevtools(
52 props: DevtoolsOptions,
53): React.ReactElement | null {
54 const queryClient = useQueryClient(props.client)
55 const ref = React.useRef<HTMLDivElement>(null)
56 const {
57 buttonPosition,
58 position,
59 initialIsOpen,
60 errorTypes,
61 styleNonce,
62 shadowDOMTarget,
63 hideDisabledQueries,
64 } = props
65 const [devtools] = React.useState(
66 new TanstackQueryDevtools({
67 client: queryClient,
68 queryFlavor: 'React Query',
69 version: '5',
70 onlineManager,
71 buttonPosition,
72 position,
73 initialIsOpen,
74 errorTypes,
75 styleNonce,
76 shadowDOMTarget,
77 hideDisabledQueries,
78 }),
79 )
80​
81 React.useEffect(() => {
82 devtools.setClient(queryClient)
83 }, [queryClient, devtools])
84​
85 React.useEffect(() => {
86 if (buttonPosition) {
87 devtools.setButtonPosition(buttonPosition)
88 }
89 }, [buttonPosition, devtools])
90​
91 React.useEffect(() => {
92 if (position) {
93 devtools.setPosition(position)
94 }
95 }, [position, devtools])
96​
97 React.useEffect(() => {
98 devtools.setInitialIsOpen(initialIsOpen || false)
99 }, [initialIsOpen, devtools])
100​
101 React.useEffect(() => {
102 devtools.setErrorTypes(errorTypes || [])
103 }, [errorTypes, devtools])
104​
105 React.useEffect(() => {
106 if (ref.current) {
107 devtools.mount(ref.current)
108 }
109​
110 return () => {
111 devtools.unmount()
112 }
113 }, [devtools])
114​
115 return <div dir="ltr" className="tsqd-parent-container" ref={ref}></div>