yohei0125 4 лет назад
Родитель
Сommit
5157cc03c8
1 измененных файлов с 17 добавлено и 12 удалено
  1. 17 12
      packages/app/src/components/SearchPage/SearchResultContent.tsx

+ 17 - 12
packages/app/src/components/SearchPage/SearchResultContent.tsx

@@ -61,6 +61,22 @@ const scrollTo = (scrollElement:HTMLElement) => {
   }
 };
 
+const MutationObserverWrapper = (scrollElement:HTMLElement) => {
+  const observerCallback = (mutationRecords) => {
+    mutationRecords.forEach((record) => {
+      const targetId = record.target.id;
+      if (targetId !== 'wiki') return;
+      scrollTo(scrollElement);
+    });
+  };
+
+  const observer = new MutationObserver(observerCallback);
+  observer.observe(scrollElement, MUTATION_OBSERVER_CONFIG);
+  return () => {
+    observer.disconnect();
+  };
+};
+
 const SearchResultContent: FC<Props> = (props: Props) => {
   const scrollElementRef = useRef(null);
 
@@ -68,18 +84,7 @@ const SearchResultContent: FC<Props> = (props: Props) => {
   useEffect(() => {
     const scrollElement = scrollElementRef.current as HTMLElement | null;
     if (scrollElement == null) return;
-    const observerCallback = (mutationRecords) => {
-      mutationRecords.forEach((record) => {
-        const targetId = record.target.id;
-        if (targetId !== 'wiki') return;
-        scrollTo(scrollElement);
-      });
-    };
-    const observer = new MutationObserver(observerCallback);
-    observer.observe(scrollElement, MUTATION_OBSERVER_CONFIG);
-    return () => {
-      observer.disconnect();
-    };
+    MutationObserverWrapper(scrollElement);
   });
   // *******************************  end  *******************************