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 * This overrides the theme selected in the devtools Settings menu. When omitted, the
58 * selected theme is used, or `'system'` if none is selected.
59 */
60 theme?: Theme
61}
62β
63/**
64 * Renders the TanStack Query devtools, with a toggle button that opens them, for the given or
65 * nearest `QueryClient`.
66 * @param props - The devtools options.
67 * @returns The element the devtools are mounted in.
68 */
69export function ReactQueryDevtools(
70 props: DevtoolsOptions,
71): React.ReactElement | null {
72 const queryClient = useQueryClient(props.client)
73 const ref = React.useRef<HTMLDivElement>(null)
74 const {
75 buttonPosition,
76 position,
77 initialIsOpen,
78 errorTypes,
79 styleNonce,
80 shadowDOMTarget,
81 hideDisabledQueries,
82 theme,
83 } = props
84 const [devtools] = React.useState(
85 () =>
86 new TanstackQueryDevtools({
87 client: queryClient,
88 queryFlavor: 'React Query',
89 version: '5',
90 onlineManager,
91 buttonPosition,
92 position,
93 initialIsOpen,
94 errorTypes,
95 styleNonce,
96 shadowDOMTarget,
97 hideDisabledQueries,
98 theme,
99 }),
100 )
101β
102 React.useEffect(() => {
103 devtools.setClient(queryClient)
104 }, [queryClient, devtools])
105β
106 React.useEffect(() => {
107 if (buttonPosition) {
108 devtools.setButtonPosition(buttonPosition)
109 }
110 }, [buttonPosition, devtools])
111β
112 React.useEffect(() => {
113 if (position) {
114 devtools.setPosition(position)
115 }
116 }, [position, devtools])
117β
118 React.useEffect(() => {
119 devtools.setInitialIsOpen(initialIsOpen || false)
120 }, [initialIsOpen, devtools])
121β
122 React.useEffect(() => {
123 devtools.setErrorTypes(errorTypes || [])
124 }, [errorTypes, devtools])
125β
126 React.useEffect(() => {
127 devtools.setTheme(theme)
128 }, [theme, devtools])
129β
130 React.useEffect(() => {
131 if (ref.current) {
132 devtools.mount(ref.current)
133 }
134β
135 return () => {
136 devtools.unmount()
137 }
138 }, [devtools])
139β
140 return <div className="tsqd-parent-container" ref={ref}></div>
141}