Skip to content

Commit f1f364c

Browse files
committed
feat: add remove method and deprecate clear
1 parent c81e309 commit f1f364c

File tree

7 files changed

+31
-6
lines changed

7 files changed

+31
-6
lines changed

docs/src/pages/docs/api.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ title: API Reference
77

88
```js
99
const {
10-
clear,
1110
data,
1211
error,
1312
failureCount,
@@ -20,6 +19,7 @@ const {
2019
isStale,
2120
isSuccess,
2221
refetch,
22+
remove,
2323
status,
2424
} = useQuery(queryKey, queryFn?, {
2525
cacheTime,
@@ -194,7 +194,7 @@ const queryInfo = useQuery({
194194
- `refetch: Function({ throwOnError }) => Promise<TResult | undefined>`
195195
- A function to manually refetch the query.
196196
- If the query errors, the error will only be logged. If you want an error to be thrown, pass the `throwOnError: true` option
197-
- `clear: Function() => void`
197+
- `remove: Function() => void`
198198
- A function to remove the query from the cache.
199199
200200
## `usePaginatedQuery`

src/core/query.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ export class Query<TResult, TError> {
146146
}
147147

148148
this.gcTimeout = setTimeout(() => {
149-
this.clear()
149+
this.remove()
150150
}, this.cacheTime)
151151
}
152152

@@ -206,7 +206,14 @@ export class Query<TResult, TError> {
206206
})
207207
}
208208

209+
/**
210+
* @deprecated
211+
*/
209212
clear(): void {
213+
this.remove()
214+
}
215+
216+
remove(): void {
210217
this.queryCache.removeQuery(this)
211218
}
212219

src/core/queryObserver.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class QueryObserver<TResult, TError> {
3030
this.initialUpdateCount = 0
3131

3232
// Bind exposed methods
33-
this.clear = this.clear.bind(this)
33+
this.remove = this.remove.bind(this)
3434
this.refetch = this.refetch.bind(this)
3535
this.fetchMore = this.fetchMore.bind(this)
3636

@@ -110,8 +110,15 @@ export class QueryObserver<TResult, TError> {
110110
return this.currentResult
111111
}
112112

113+
/**
114+
* @deprecated
115+
*/
113116
clear(): void {
114-
return this.currentQuery.clear()
117+
this.remove()
118+
}
119+
120+
remove(): void {
121+
this.currentQuery.remove()
115122
}
116123

117124
refetch(options?: RefetchOptions): Promise<TResult | undefined> {
@@ -234,7 +241,7 @@ export class QueryObserver<TResult, TError> {
234241
this.currentResult = {
235242
...getStatusProps(status),
236243
canFetchMore: state.canFetchMore,
237-
clear: this.clear,
244+
clear: this.remove,
238245
data,
239246
error: state.error,
240247
failureCount: state.failureCount,
@@ -247,6 +254,7 @@ export class QueryObserver<TResult, TError> {
247254
isPreviousData,
248255
isStale: this.isStale,
249256
refetch: this.refetch,
257+
remove: this.remove,
250258
updatedAt,
251259
}
252260
}

src/core/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ export interface QueryResultBase<TResult, TError = unknown> {
207207
isStale: boolean
208208
isSuccess: boolean
209209
refetch: (options?: RefetchOptions) => Promise<TResult | undefined>
210+
remove: () => void
210211
status: QueryStatus
211212
updatedAt: number
212213
}

src/react/tests/useInfiniteQuery.test.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ describe('useInfiniteQuery', () => {
7878
isStale: true,
7979
isSuccess: false,
8080
refetch: expect.any(Function),
81+
remove: expect.any(Function),
8182
status: 'loading',
8283
updatedAt: expect.any(Number),
8384
})
@@ -107,6 +108,7 @@ describe('useInfiniteQuery', () => {
107108
isStale: true,
108109
isSuccess: true,
109110
refetch: expect.any(Function),
111+
remove: expect.any(Function),
110112
status: 'success',
111113
updatedAt: expect.any(Number),
112114
})

src/react/tests/usePaginatedQuery.test.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ describe('usePaginatedQuery', () => {
5050
latestData: undefined,
5151
resolvedData: undefined,
5252
refetch: expect.any(Function),
53+
remove: expect.any(Function),
5354
status: 'loading',
5455
updatedAt: expect.any(Number),
5556
})
@@ -75,6 +76,7 @@ describe('usePaginatedQuery', () => {
7576
latestData: 1,
7677
resolvedData: 1,
7778
refetch: expect.any(Function),
79+
remove: expect.any(Function),
7880
status: 'success',
7981
updatedAt: expect.any(Number),
8082
})

src/react/tests/useQuery.test.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ describe('useQuery', () => {
140140
isStale: true,
141141
isSuccess: false,
142142
refetch: expect.any(Function),
143+
remove: expect.any(Function),
143144
status: 'loading',
144145
updatedAt: expect.any(Number),
145146
})
@@ -163,6 +164,7 @@ describe('useQuery', () => {
163164
isStale: true,
164165
isSuccess: true,
165166
refetch: expect.any(Function),
167+
remove: expect.any(Function),
166168
status: 'success',
167169
updatedAt: expect.any(Number),
168170
})
@@ -216,6 +218,7 @@ describe('useQuery', () => {
216218
isStale: true,
217219
isSuccess: false,
218220
refetch: expect.any(Function),
221+
remove: expect.any(Function),
219222
status: 'loading',
220223
updatedAt: expect.any(Number),
221224
})
@@ -239,6 +242,7 @@ describe('useQuery', () => {
239242
isStale: true,
240243
isSuccess: false,
241244
refetch: expect.any(Function),
245+
remove: expect.any(Function),
242246
status: 'loading',
243247
updatedAt: expect.any(Number),
244248
})
@@ -262,6 +266,7 @@ describe('useQuery', () => {
262266
isStale: true,
263267
isSuccess: false,
264268
refetch: expect.any(Function),
269+
remove: expect.any(Function),
265270
status: 'error',
266271
updatedAt: expect.any(Number),
267272
})

0 commit comments

Comments
 (0)