feat(react-query): add 'QueryStrictMode' #12059
- @@ -0,0 +256,21 @@256+ }, [logger, throwOnUnusedPrefetch])257+258+ React.useEffect(() => {259+ if (isNested || isRestoring) {260+ return261+ }262+263+ const timers = new Map<264+ Query<any, any, any, any>,265+ ReturnType<typeof setTimeout>266+ >()267+268+ const clearTimer = (query: Query<any, any, any, any>) => {269+ clearTimeout(timers.get(query))270+ timers.delete(query)271+ }272+273+ const isUnused = (query: Query<any, any, any, any>) =>274+ !used.has(query) && query.getObserversCount() === 0275+276+ const unsubscribe = client.getQueryCache().subscribe((event) => {
TkDodo reviewed
@@ -0,0 +1,16 @@1+'use client'2+import * as React from 'react'3+4+import { useQueryClient } from './QueryClientProvider'5+import { useIsRestoring } from './IsRestoringProvider'6+import type { Query, QueryClient } from '@tanstack/query-core'7+8+/**9+ * A problem reported by `QueryStrictMode`.10+ */11+export interface QueryStrictModeEvent {12+ /**13+ * `'missing-prefetch'` when a suspense hook starts a fetch during render, and `'unused-prefetch'`14+ * when nothing used a prefetched query within `unusedPrefetchTimeout`.15+ */16+ type: 'missing-prefetch' | 'unused-prefetch'@@ -0,0 +8,21 @@8+/**9+ * A problem reported by `QueryStrictMode`.10+ */11+export interface QueryStrictModeEvent {12+ /**13+ * `'missing-prefetch'` when a suspense hook starts a fetch during render, and `'unused-prefetch'`14+ * when nothing used a prefetched query within `unusedPrefetchTimeout`.15+ */16+ type: 'missing-prefetch' | 'unused-prefetch'17+ /**18+ * A readable description of the problem.19+ */20+ message: string21+ /**22+ * The query the problem was found for.23+ */24+ query: Query<any, any, any, any>25+}26+27+/**28+ * Thrown instead of logging a problem when `throwOnMissingPrefetch` or `throwOnUnusedPrefetch`@@ -0,0 +89,21 @@89+ * @returns Whether to throw `event`.90+ */91+export const shouldThrowQueryStrictModeEvent = (92+ option: QueryStrictModeThrowOption,93+ event: QueryStrictModeEvent,94+) => (typeof option === 'function' ? option(event) : option)95+96+/**97+ * The props accepted by `QueryStrictMode`.98+ */99+export interface QueryStrictModeProps {100+ /**101+ * The subtree to check.102+ */103+ children?: React.ReactNode104+ /**105+ * Receives every problem that is found. Use it to filter out known or intentional waterfalls, for106+ * example by `queryKey` or `meta`.107+ * @defaultValue Logs `event.message` with `console.error`.108+ */109+ logger?: QueryStrictModeLogger@@ -128,3 +131,12 @@128131 observer.setOptions(defaultedOptions)129132 }, [defaultedOptions, observer])130133134+ const willSuspend = !!shouldSuspend(defaultedOptions, result)135+136+ if (!isRestoring) {137+ checkQueryStrictMode(138+ strictMode,139+ client.getQueryCache().get(defaultedOptions.queryHash),140+ willSuspend,141+ )142+ }@@ -451,3 +454,4 @@451454 )452455 }, [defaultedQueries, options, observer])453456457+ if (!isRestoring) {- packages/react-query/src/QueryStrictMode.tsx lines 276 to 290OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -0,0 +270,21 @@270+}: QueryStrictModeProps) => {271+ const client = useQueryClient(queryClient)272+ const isRestoring = useIsRestoring()273+ const parent = useQueryStrictMode()274+ const isNested = parent !== null275+276+ const [{ used, checked, markUsed: ownMarkUsed }] = React.useState(() => {277+ const queries = new WeakSet<Query<any, any, any, any>>()278+ return {279+ used: queries,280+ // When the prefetch was recorded that each query was last checked for, so a timer that281+ // already ran doesn't start again when the effect runs again282+ checked: new WeakMap<Query<any, any, any, any>, number>(),283+ markUsed: (query: Query<any, any, any, any>) => {284+ queries.add(query)285+ },286+ }287+ })288+ // A nested `QueryStrictMode` marks queries in the tracking of its ancestor, so unused prefetches289+ // are only reported once290+ const markUsed = isNested ? parent.markUsed : ownMarkUsed
Sign in to join this conversation.
🦋 Changeset detected#
Latest commit: c242694ccba754ef1cdb1789b81153dbbd70d40a
The changes in this PR will be included in the next version bump.
This PR includes changesets to release 24 packages
Not sure what this means? Click here to learn what changesets are.
Click here if you're a maintainer who wants to add another changeset to this PR