Procházet zdrojové kódy

Merge pull request #7285 from weseek/fix/113838-refactor-useEditingMarkdown

fix: Previous editing markdown remains after changing page
Yuki Takei před 3 roky
rodič
revize
9a62807a63

+ 3 - 2
packages/app/src/stores/editor.tsx

@@ -13,12 +13,13 @@ import {
   useCurrentUser, useDefaultIndentSize, useIsGuestUser,
 } from './context';
 // import { localStorageMiddleware } from './middlewares/sync-to-storage';
-import { useSWRxTagsInfo } from './page';
+import { useCurrentPagePath, useSWRxTagsInfo } from './page';
 import { useStaticSWR } from './use-static-swr';
 
 
 export const useEditingMarkdown = (initialData?: string): SWRResponse<string, Error> => {
-  return useStaticSWR('editingMarkdown', initialData);
+  const { data: currentPagePath } = useCurrentPagePath();
+  return useStaticSWR(['editingMarkdown', currentPagePath], initialData);
 };
 
 

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

@@ -3,7 +3,7 @@ import { useEffect } from 'react';
 import assert from 'assert';
 
 import {
-  Key, SWRConfiguration, SWRResponse,
+  mutate, Key, SWRConfiguration, SWRResponse,
 } from 'swr';
 import useSWRImmutable from 'swr/immutable';
 
@@ -27,10 +27,9 @@ export function useStaticSWR<Data, Error>(
   // Do mutate with `data` from args
   useEffect(() => {
     if (data !== undefined) {
-      swrResponse.mutate(data);
+      mutate(key, data);
     }
-  // eslint-disable-next-line react-hooks/exhaustive-deps
-  }, [data]); // Only depends on `data`
+  }, [data, key]);
 
   return swrResponse;
 }