Просмотр исходного кода

optimizing performance with useCallback

kosei-n 2 лет назад
Родитель
Сommit
1fda11e7f0
1 измененных файлов с 11 добавлено и 11 удалено
  1. 11 11
      apps/app/src/components/PageSelectModal/PageSelectModal.tsx

+ 11 - 11
apps/app/src/components/PageSelectModal/PageSelectModal.tsx

@@ -1,5 +1,5 @@
 import type { FC } from 'react';
-import { useState } from 'react';
+import { useState, useCallback } from 'react';
 
 import nodePath from 'path';
 
@@ -42,13 +42,9 @@ export const PageSelectModal: FC = () => {
 
   const targetPathOrId = targetId || currentPath;
 
-  if (isGuestUser == null) {
-    return null;
-  }
-
   const path = currentPath || '/';
 
-  const onClickTreeItem = (page: IPageForItem) => {
+  const onClickTreeItem = useCallback((page: IPageForItem) => {
     const parentPagePath = page.path;
 
     if (parentPagePath == null) {
@@ -56,21 +52,25 @@ export const PageSelectModal: FC = () => {
     }
 
     setClickedParentPagePath(parentPagePath);
-  };
+  }, []);
 
-  const onClickCancel = () => {
+  const onClickCancel = useCallback(() => {
     setClickedParentPagePath('');
     closeModal();
-  };
+  }, [closeModal]);
 
-  const onClickDone = () => {
+  const onClickDone = useCallback(() => {
     const currentPageTitle = nodePath.basename(currentPage?.path ?? '') || '/';
     const newPagePath = nodePath.resolve(clickedParentPagePath, currentPageTitle);
 
     pagePathRenameHandler(newPagePath);
 
     closeModal();
-  };
+  }, [clickedParentPagePath, closeModal, currentPage?.path, pagePathRenameHandler]);
+
+  if (isGuestUser == null) {
+    return null;
+  }
 
   return (
     <Modal