Repository: tannerlinsley/react-query
Pull request #1022 (2020-09-16 06:36:36)

feat: add remove method and deprecate clear

vercel[bot]: This pull request is being automatically deployed with Vercel ([learn more](https://vercel.link/github-learn-more)).  
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: [https://vercel.com/tannerlinsley/react-query/21copcxt3](https://vercel.com/tannerlinsley/react-query/21copcxt3)  
✅ Preview: *In Progress*



Patch:
diff --git a/docs/src/pages/docs/api.md b/docs/src/pages/docs/api.md
index 974abcc91..39237b634 100644
--- a/docs/src/pages/docs/api.md
+++ b/docs/src/pages/docs/api.md
@@ -7,7 +7,6 @@ title: API Reference
 
 ```js
 const {
-  clear,
   data,
   error,
   failureCount,
@@ -20,6 +19,7 @@ const {
   isStale,
   isSuccess,
   refetch,
+  remove,
   status,
 } = useQuery(queryKey, queryFn?, {
   cacheTime,
@@ -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`
diff --git a/src/core/query.ts b/src/core/query.ts
index b8d50fa30..a5f255198 100644
--- a/src/core/query.ts
+++ b/src/core/query.ts
@@ -146,7 +146,7 @@ export class Query<TResult, TError> {
     }
 
     this.gcTimeout = setTimeout(() => {
-      this.clear()
+      this.remove()
     }, this.cacheTime)
   }
 
@@ -206,7 +206,17 @@ export class Query<TResult, TError> {
     })
   }
 
+  /**
+   * @deprecated
+   */
   clear(): void {
+    Console.warn(
+      'react-query: clear() has been deprecated, please use remove() instead'
+    )
+    this.remove()
+  }
+
+  remove(): void {
     this.queryCache.removeQuery(this)
   }
 
diff --git a/src/core/queryObserver.ts b/src/core/queryObserver.ts
index cbe1366f9..b48d8d48f 100644
--- a/src/core/queryObserver.ts
+++ b/src/core/queryObserver.ts
@@ -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)
 
@@ -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> {
@@ -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,
@@ -247,6 +254,7 @@ export class QueryObserver<TResult, TError> {
       isPreviousData,
       isStale: this.isStale,
       refetch: this.refetch,
+      remove: this.remove,
       updatedAt,
     }
   }
diff --git a/src/core/types.ts b/src/core/types.ts
index 0f3f636d0..e86030300 100644
--- a/src/core/types.ts
+++ b/src/core/types.ts
@@ -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
 }


Test patch:
diff --git a/src/react/tests/useInfiniteQuery.test.tsx b/src/react/tests/useInfiniteQuery.test.tsx
index a85f536d7..ef3d417ca 100644
--- a/src/react/tests/useInfiniteQuery.test.tsx
+++ b/src/react/tests/useInfiniteQuery.test.tsx
@@ -78,6 +78,7 @@ describe('useInfiniteQuery', () => {
       isStale: true,
       isSuccess: false,
       refetch: expect.any(Function),
+      remove: expect.any(Function),
       status: 'loading',
       updatedAt: expect.any(Number),
     })
@@ -107,6 +108,7 @@ describe('useInfiniteQuery', () => {
       isStale: true,
       isSuccess: true,
       refetch: expect.any(Function),
+      remove: expect.any(Function),
       status: 'success',
       updatedAt: expect.any(Number),
     })
diff --git a/src/react/tests/usePaginatedQuery.test.tsx b/src/react/tests/usePaginatedQuery.test.tsx
index 09aa22a08..1f18610d4 100644
--- a/src/react/tests/usePaginatedQuery.test.tsx
+++ b/src/react/tests/usePaginatedQuery.test.tsx
@@ -50,6 +50,7 @@ describe('usePaginatedQuery', () => {
       latestData: undefined,
       resolvedData: undefined,
       refetch: expect.any(Function),
+      remove: expect.any(Function),
       status: 'loading',
       updatedAt: expect.any(Number),
     })
@@ -75,6 +76,7 @@ describe('usePaginatedQuery', () => {
       latestData: 1,
       resolvedData: 1,
       refetch: expect.any(Function),
+      remove: expect.any(Function),
       status: 'success',
       updatedAt: expect.any(Number),
     })
diff --git a/src/react/tests/useQuery.test.tsx b/src/react/tests/useQuery.test.tsx
index 8420627a8..17179d6f8 100644
--- a/src/react/tests/useQuery.test.tsx
+++ b/src/react/tests/useQuery.test.tsx
@@ -140,6 +140,7 @@ describe('useQuery', () => {
       isStale: true,
       isSuccess: false,
       refetch: expect.any(Function),
+      remove: expect.any(Function),
       status: 'loading',
       updatedAt: expect.any(Number),
     })
@@ -163,6 +164,7 @@ describe('useQuery', () => {
       isStale: true,
       isSuccess: true,
       refetch: expect.any(Function),
+      remove: expect.any(Function),
       status: 'success',
       updatedAt: expect.any(Number),
     })
@@ -216,6 +218,7 @@ describe('useQuery', () => {
       isStale: true,
       isSuccess: false,
       refetch: expect.any(Function),
+      remove: expect.any(Function),
       status: 'loading',
       updatedAt: expect.any(Number),
     })
@@ -239,6 +242,7 @@ describe('useQuery', () => {
       isStale: true,
       isSuccess: false,
       refetch: expect.any(Function),
+      remove: expect.any(Function),
       status: 'loading',
       updatedAt: expect.any(Number),
     })
@@ -262,6 +266,7 @@ describe('useQuery', () => {
       isStale: true,
       isSuccess: false,
       refetch: expect.any(Function),
+      remove: expect.any(Function),
       status: 'error',
       updatedAt: expect.any(Number),
     })