Taichi Masuyama 4 سال پیش
والد
کامیت
5a05f2a40f
3فایلهای تغییر یافته به همراه33 افزوده شده و 4 حذف شده
  1. 7 0
      packages/app/src/stores/context.tsx
  2. 26 0
      packages/app/src/stores/use-static-swr.tsx
  3. 0 4
      packages/app/src/utils/swr-utils.ts

+ 7 - 0
packages/app/src/stores/context.tsx

@@ -0,0 +1,7 @@
+import { SWRResponse } from 'swr';
+import { useStaticSWR } from './use-static-swr';
+
+type Hoge = any;
+export const useHoge = (initialData?: Hoge): SWRResponse<Hoge, Error> => {
+  return useStaticSWR<Hoge, Error>('hoge', initialData || null);
+};

+ 26 - 0
packages/app/src/stores/use-static-swr.tsx

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

+ 0 - 4
packages/app/src/utils/swr-utils.ts

@@ -1,9 +1,5 @@
 import { SWRConfiguration } from 'swr';
 
-import axios from './axios';
-
 export const swrGlobalConfiguration: SWRConfiguration = {
-  fetcher: url => axios.get(url).then(res => res.data),
-  revalidateOnFocus: false,
   errorRetryCount: 1,
 };