Browse Source

fix(js): Correct eslint hook rule in useApi (#34474)

Evan Purkhiser 2 years ago
parent
commit
09a4448710
1 changed files with 8 additions and 5 deletions
  1. 8 5
      static/app/utils/useApi.tsx

+ 8 - 5
static/app/utils/useApi.tsx

@@ -1,4 +1,4 @@
-import {useEffect, useRef} from 'react';
+import {useCallback, useEffect, useRef} from 'react';
 
 import {Client} from 'sentry/api';
 
@@ -36,11 +36,14 @@ function useApi({persistInFlight, api: providedApi}: Options = {}) {
   // Use the provided client if available
   const api = providedApi ?? localApi.current!;
 
-  function handleCleanup() {
-    !persistInFlight && api.clear();
-  }
+  // Clear API calls on unmount (if persistInFlight is disabled
+  const clearOnUnmount = useCallback(() => {
+    if (!persistInFlight) {
+      api.clear();
+    }
+  }, [api, persistInFlight]);
 
-  useEffect(() => handleCleanup, []);
+  useEffect(() => clearOnUnmount, [clearOnUnmount]);
 
   return api;
 }