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