Open History to see the last commit
1'use client'
2import { TanstackQueryDevtools } from '@tanstack/query-devtools'
3import { onlineManager, useQueryClient } from '@tanstack/react-query'
4import * as React from 'react'
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 * @deprecated The devtools no longer add a `<style>` element, so they need no
44 * nonce. This option does nothing.
45 */
46 styleNonce?: string
47 /**
48 * The shadow root to add the devtools styles to. By default, the styles are
49 * added to the shadow root the devtools render in, or to the document when
50 * they do not render in a shadow root.
51 */
52 shadowDOMTarget?: ShadowRoot
53 /**
54 * The Trusted Types policy the devtools use to create their HTML templates.
55 * Pass one when your page enforces `require-trusted-types-for 'script'`.
56 * The devtools only pass it their own markup.
57 */
58 trustedTypesPolicy?: { createHTML: (html: string) => unknown }
59 /**
60 * Set this to true to hide disabled queries from the devtools panel.
61 */
62 hideDisabledQueries?: boolean
63 /**
64 * Set this to 'light', 'dark', or 'system' to change the theme of the devtools panel.
65 * This overrides the theme selected in the devtools Settings menu. When omitted, the
66 * selected theme is used, or `'system'` if none is selected.
67 */
68 theme?: Theme
69}
70β
71/**
72 * Renders the TanStack Query devtools, with a toggle button that opens them, for the given or
73 * nearest `QueryClient`.
74 * @param props - The devtools options.
75 * @returns The element the devtools are mounted in.
76 */
77export function ReactQueryDevtools(
78 props: DevtoolsOptions,
79): React.ReactElement | null {
80 const queryClient = useQueryClient(props.client)
81 const ref = React.useRef<HTMLDivElement>(null)
82 const {
83 buttonPosition,
84 position,
85 initialIsOpen,
86 errorTypes,
87 shadowDOMTarget,
88 trustedTypesPolicy,
89 hideDisabledQueries,
90 theme,
91 } = props
92 const [devtools] = React.useState(
93 () =>
94 new TanstackQueryDevtools({
95 client: queryClient,
96 queryFlavor: 'React Query',
97 version: '5',
98 onlineManager,
99 buttonPosition,
100 position,
101 initialIsOpen,
102 errorTypes,
103 shadowDOMTarget,
104 trustedTypesPolicy,
105 hideDisabledQueries,
106 theme,
107 }),
108 )
109β
110 React.useEffect(() => {
111 devtools.setClient(queryClient)
112 }, [queryClient, devtools])
113β
114 React.useEffect(() => {
115 if (buttonPosition) {
116 devtools.setButtonPosition(buttonPosition)
117 }
118 }, [buttonPosition, devtools])
119β
120 React.useEffect(() => {
121 if (position) {
122 devtools.setPosition(position)
123 }
124 }, [position, devtools])
125β
126 React.useEffect(() => {
127 devtools.setInitialIsOpen(initialIsOpen || false)
128 }, [initialIsOpen, devtools])
129β
130 React.useEffect(() => {
131 devtools.setErrorTypes(errorTypes || [])
132 }, [errorTypes, devtools])
133β
134 React.useEffect(() => {
135 devtools.setTheme(theme)
136 }, [theme, devtools])
137β
138 React.useEffect(() => {
139 if (ref.current) {
140 devtools.mount(ref.current)
141 }
142β
143 return () => {
144 devtools.unmount()
145 }
146 }, [devtools])
147β
148 return <div className="tsqd-parent-container" ref={ref}></div>
149}