Skip to content

Commit 761a1a7

Browse files
committed
feat: notifyOnChangeTracked
different approach to tracking used props by using an option rather than a new hook, so that we can also easily combine this with useInfiniteQuery; also, using Object.defineProperty rather than a Proxy
1 parent c7e3f5e commit 761a1a7

File tree

6 files changed

+31
-87
lines changed

6 files changed

+31
-87
lines changed

src/core/queryObserver.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,34 @@ export class QueryObserver<
149149
}
150150

151151
createQueryResult(
152-
result: QueryObserverResult<TData, TError>
152+
queryObserverResult: QueryObserverResult<TData, TError>
153153
): QueryObserverResult<TData, TError> {
154-
return result
154+
if (this.options.notifyOnChangeTracked) {
155+
const trackedResult = {}
156+
const addNotifyOnChangeProps = (prop: keyof QueryObserverResult) => {
157+
if (!this.options.notifyOnChangeProps) {
158+
this.options.notifyOnChangeProps = []
159+
}
160+
161+
if (!this.options.notifyOnChangeProps.includes(prop)) {
162+
this.options.notifyOnChangeProps.push(prop)
163+
}
164+
}
165+
166+
Object.keys(queryObserverResult).forEach(key => {
167+
Object.defineProperty(trackedResult, key, {
168+
configurable: false,
169+
enumerable: true,
170+
get() {
171+
addNotifyOnChangeProps(key as keyof QueryObserverResult)
172+
return queryObserverResult[key as keyof QueryObserverResult]
173+
},
174+
})
175+
})
176+
177+
return trackedResult as QueryObserverResult<TData, TError>
178+
}
179+
return queryObserverResult
155180
}
156181

157182
setOptions(

src/core/trackedQueryObserver.ts

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/core/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ export interface QueryObserverOptions<
142142
* If set, the component will not re-render if any of the listed properties change.
143143
*/
144144
notifyOnChangePropsExclusions?: Array<keyof InfiniteQueryObserverResult>
145+
/**
146+
* If set, access to properties will be tracked and only re-rendered if one of the tracked properties change.
147+
*/
148+
notifyOnChangeTracked?: boolean
145149
/**
146150
* This callback will fire any time the query successfully fetches new data.
147151
*/

src/react/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ export {
1010
export { useIsFetching } from './useIsFetching'
1111
export { useMutation } from './useMutation'
1212
export { useQuery } from './useQuery'
13-
export { useTrackedQuery } from './useTrackedQuery'
1413
export { useQueries } from './useQueries'
1514
export { useInfiniteQuery } from './useInfiniteQuery'
1615

src/react/types.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,6 @@ export interface UseQueryOptions<
2121
TData = TQueryFnData
2222
> extends UseBaseQueryOptions<TQueryFnData, TError, TData> {}
2323

24-
export type UseTrackedQueryOptions<
25-
TQueryFnData = unknown,
26-
TError = unknown,
27-
TData = TQueryFnData
28-
> = Omit<
29-
UseQueryOptions<TQueryFnData, TError, TData>,
30-
'notifyOnChangeProps' | 'notifyOnChangePropsExclusions'
31-
>
32-
3324
export interface UseInfiniteQueryOptions<
3425
TQueryFnData = unknown,
3526
TError = unknown,

src/react/useTrackedQuery.ts

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)