Skip to content

feat(devtools): Make queryclient reactive #5366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/query-devtools/src/Devtools.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1844,7 +1844,7 @@ const getStyles = () => {
background-color: ${colors.darkGray[400]};
font-size: ${font.size.sm};
color: ${colors.gray[500]};
z-index: 2;
z-index: 7;
`,
settingsMenuHeader: css`
padding: ${tokens.size[1.5]} ${tokens.size[2.5]};
Expand Down
13 changes: 10 additions & 3 deletions packages/query-devtools/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export type { DevtoolsButtonPosition, DevtoolsPosition, DevToolsErrorType }
export interface TanstackQueryDevtoolsConfig extends QueryDevtoolsProps {}

class TanstackQueryDevtools {
client: QueryClient
client: Signal<QueryClient>
onlineManager: typeof TonlineManager
queryFlavor: string
version: string
Expand All @@ -39,7 +39,7 @@ class TanstackQueryDevtools {
initialIsOpen,
errorTypes,
} = config
this.client = client
this.client = createSignal(client)
this.queryFlavor = queryFlavor
this.version = version
this.onlineManager = onlineManager
Expand All @@ -65,6 +65,10 @@ class TanstackQueryDevtools {
this.errorTypes[1](errorTypes)
}

setClient(client: QueryClient) {
this.client[1](client)
}

mount<T extends HTMLElement>(el: T) {
if (this.isMounted) {
throw new Error('Devtools is already mounted')
Expand All @@ -74,13 +78,16 @@ class TanstackQueryDevtools {
const [pos] = this.position
const [isOpen] = this.initialIsOpen
const [errors] = this.errorTypes
const [queryClient] = this.client
return (
<DevtoolsComponent
client={this.client}
queryFlavor={this.queryFlavor}
version={this.version}
onlineManager={this.onlineManager}
{...{
get client() {
return queryClient()
},
get buttonPosition() {
return btnPosition()
},
Expand Down
7 changes: 6 additions & 1 deletion packages/react-query-devtools/src/devtools.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ export function ReactQueryDevtools(
props: DevtoolsOptions,
): React.ReactElement | null {
const queryClient = useQueryClient()
const client = props.client || queryClient
Comment on lines 42 to +43
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useQueryClient can handle this internally, so we can do:

const client = useQueryClient(props.client)

useQueryClient will read from context or return the passed prop if it isn't undefined:

export const useQueryClient = (queryClient?: QueryClient) => {
const client = React.useContext(QueryClientContext)
if (queryClient) {
return queryClient
}
if (!client) {
throw new Error('No QueryClient set, use QueryClientProvider to set one')
}
return client
}

const ref = useRef<HTMLDivElement>(null)
const { buttonPosition, position, initialIsOpen, errorTypes } = props
const [devtools] = useState(
new TanstackQueryDevtools({
client: props.client || queryClient,
client: client,
queryFlavor: 'React Query',
version: '5',
onlineManager,
Expand All @@ -55,6 +56,10 @@ export function ReactQueryDevtools(
}),
)

useEffect(() => {
devtools.setClient(client)
}, [client, devtools])

useEffect(() => {
if (buttonPosition) {
devtools.setButtonPosition(buttonPosition)
Expand Down