Kaynağa Gözat

improve useStaticSWR to use mutate which is configured by middleware

Yuki Takei 4 yıl önce
ebeveyn
işleme
8e7251eeb2

+ 2 - 4
packages/app/src/stores/ui.tsx

@@ -1,4 +1,4 @@
-import useSWR, {
+import {
   useSWRConfig, SWRResponse, Key, Fetcher, Middleware,
   useSWRConfig, SWRResponse, Key, Fetcher, Middleware,
 } from 'swr';
 } from 'swr';
 import useSWRImmutable from 'swr/immutable';
 import useSWRImmutable from 'swr/immutable';
@@ -168,13 +168,11 @@ export const useDrawerMode = (): SWRResponse<boolean, Error> => {
     return isDeviceSmallerThanMd || preferDrawerMode;
     return isDeviceSmallerThanMd || preferDrawerMode;
   };
   };
 
 
-  return useSWR(
+  return useSWRImmutable(
     condition ? [editorMode, preferDrawerModeByUser, preferDrawerModeOnEditByUser, isDeviceSmallerThanMd] : null,
     condition ? [editorMode, preferDrawerModeByUser, preferDrawerModeOnEditByUser, isDeviceSmallerThanMd] : null,
     calcDrawerMode,
     calcDrawerMode,
     {
     {
       fallback: calcDrawerMode,
       fallback: calcDrawerMode,
-      revalidateOnFocus: false,
-      revalidateOnReconnect: false,
     },
     },
   );
   );
 };
 };

+ 7 - 3
packages/app/src/stores/use-static-swr.tsx

@@ -1,5 +1,5 @@
 import {
 import {
-  Key, SWRConfiguration, SWRResponse, mutate,
+  Key, SWRConfiguration, SWRResponse,
 } from 'swr';
 } from 'swr';
 import useSWRImmutable from 'swr/immutable';
 import useSWRImmutable from 'swr/immutable';
 import { Fetcher } from 'swr/dist/types';
 import { Fetcher } from 'swr/dist/types';
@@ -17,10 +17,14 @@ export function useStaticSWR<Data, Error>(
 ): SWRResponse<Data, Error> {
 ): SWRResponse<Data, Error> {
   const [key, fetcher, configuration] = args;
   const [key, fetcher, configuration] = args;
 
 
+  const swrResponse = useSWRImmutable(key, null, configuration);
+
+  // mutate
   const fetcherFixed = fetcher || configuration?.fetcher;
   const fetcherFixed = fetcher || configuration?.fetcher;
   if (fetcherFixed != null) {
   if (fetcherFixed != null) {
-    mutate(key, fetcherFixed);
+    const { mutate } = swrResponse;
+    mutate(fetcherFixed);
   }
   }
 
 
-  return useSWRImmutable(key, null, configuration);
+  return swrResponse;
 }
 }