Skip to content
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

feat: add remove method and deprecate clear #1022

Merged
merged 1 commit into from
Sep 16, 2020
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
4 changes: 2 additions & 2 deletions docs/src/pages/docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ title: API Reference

```js
const {
clear,
data,
error,
failureCount,
Expand All @@ -20,6 +19,7 @@ const {
isStale,
isSuccess,
refetch,
remove,
status,
} = useQuery(queryKey, queryFn?, {
cacheTime,
Expand Down Expand Up @@ -194,7 +194,7 @@ const queryInfo = useQuery({
- `refetch: Function({ throwOnError }) => Promise<TResult | undefined>`
- A function to manually refetch the query.
- If the query errors, the error will only be logged. If you want an error to be thrown, pass the `throwOnError: true` option
- `clear: Function() => void`
- `remove: Function() => void`
- A function to remove the query from the cache.

## `usePaginatedQuery`
Expand Down
12 changes: 11 additions & 1 deletion src/core/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export class Query<TResult, TError> {
}

this.gcTimeout = setTimeout(() => {
this.clear()
this.remove()
}, this.cacheTime)
}

Expand Down Expand Up @@ -206,7 +206,17 @@ export class Query<TResult, TError> {
})
}

/**
* @deprecated
*/
clear(): void {
tannerlinsley marked this conversation as resolved.
Show resolved Hide resolved
Console.warn(
'react-query: clear() has been deprecated, please use remove() instead'
)
this.remove()
}

remove(): void {
this.queryCache.removeQuery(this)
}

Expand Down
14 changes: 11 additions & 3 deletions src/core/queryObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class QueryObserver<TResult, TError> {
this.initialUpdateCount = 0

// Bind exposed methods
this.clear = this.clear.bind(this)
this.remove = this.remove.bind(this)
this.refetch = this.refetch.bind(this)
this.fetchMore = this.fetchMore.bind(this)

Expand Down Expand Up @@ -110,8 +110,15 @@ export class QueryObserver<TResult, TError> {
return this.currentResult
}

/**
* @deprecated
*/
clear(): void {
return this.currentQuery.clear()
this.remove()
}

remove(): void {
this.currentQuery.remove()
}

refetch(options?: RefetchOptions): Promise<TResult | undefined> {
Expand Down Expand Up @@ -234,7 +241,7 @@ export class QueryObserver<TResult, TError> {
this.currentResult = {
...getStatusProps(status),
canFetchMore: state.canFetchMore,
clear: this.clear,
clear: this.remove,
data,
error: state.error,
failureCount: state.failureCount,
Expand All @@ -247,6 +254,7 @@ export class QueryObserver<TResult, TError> {
isPreviousData,
isStale: this.isStale,
refetch: this.refetch,
remove: this.remove,
updatedAt,
}
}
Expand Down
1 change: 1 addition & 0 deletions src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ export interface QueryResultBase<TResult, TError = unknown> {
isStale: boolean
isSuccess: boolean
refetch: (options?: RefetchOptions) => Promise<TResult | undefined>
remove: () => void
status: QueryStatus
updatedAt: number
}
Expand Down
2 changes: 2 additions & 0 deletions src/react/tests/useInfiniteQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ describe('useInfiniteQuery', () => {
isStale: true,
isSuccess: false,
refetch: expect.any(Function),
remove: expect.any(Function),
status: 'loading',
updatedAt: expect.any(Number),
})
Expand Down Expand Up @@ -107,6 +108,7 @@ describe('useInfiniteQuery', () => {
isStale: true,
isSuccess: true,
refetch: expect.any(Function),
remove: expect.any(Function),
status: 'success',
updatedAt: expect.any(Number),
})
Expand Down
2 changes: 2 additions & 0 deletions src/react/tests/usePaginatedQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ describe('usePaginatedQuery', () => {
latestData: undefined,
resolvedData: undefined,
refetch: expect.any(Function),
remove: expect.any(Function),
status: 'loading',
updatedAt: expect.any(Number),
})
Expand All @@ -75,6 +76,7 @@ describe('usePaginatedQuery', () => {
latestData: 1,
resolvedData: 1,
refetch: expect.any(Function),
remove: expect.any(Function),
status: 'success',
updatedAt: expect.any(Number),
})
Expand Down
5 changes: 5 additions & 0 deletions src/react/tests/useQuery.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ describe('useQuery', () => {
isStale: true,
isSuccess: false,
refetch: expect.any(Function),
remove: expect.any(Function),
status: 'loading',
updatedAt: expect.any(Number),
})
Expand All @@ -163,6 +164,7 @@ describe('useQuery', () => {
isStale: true,
isSuccess: true,
refetch: expect.any(Function),
remove: expect.any(Function),
status: 'success',
updatedAt: expect.any(Number),
})
Expand Down Expand Up @@ -216,6 +218,7 @@ describe('useQuery', () => {
isStale: true,
isSuccess: false,
refetch: expect.any(Function),
remove: expect.any(Function),
status: 'loading',
updatedAt: expect.any(Number),
})
Expand All @@ -239,6 +242,7 @@ describe('useQuery', () => {
isStale: true,
isSuccess: false,
refetch: expect.any(Function),
remove: expect.any(Function),
status: 'loading',
updatedAt: expect.any(Number),
})
Expand All @@ -262,6 +266,7 @@ describe('useQuery', () => {
isStale: true,
isSuccess: false,
refetch: expect.any(Function),
remove: expect.any(Function),
status: 'error',
updatedAt: expect.any(Number),
})
Expand Down