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

fix: memoize pagePath to prevent unnecessary re-renders in Page component

Yuki Takei 3 месяцев назад
Родитель
Сommit
8924046c74
1 измененных файлов с 6 добавлено и 5 удалено
  1. 6 5
      apps/app/src/pages/[[...path]]/index.page.tsx

+ 6 - 5
apps/app/src/pages/[[...path]]/index.page.tsx

@@ -1,12 +1,9 @@
-import type React from 'react';
 import type { JSX, ReactNode } from 'react';
-import { useEffect } from 'react';
+import { useEffect, useMemo } from 'react';
 import type { GetServerSideProps, GetServerSidePropsContext } from 'next';
 import dynamic from 'next/dynamic';
 import Head from 'next/head';
-import EventEmitter from 'node:events';
 import { isIPageInfo } from '@growi/core';
-import { isClient } from '@growi/core/dist/utils';
 
 // biome-ignore-start lint/style/noRestrictedImports: no-problem lazy loaded components
 import { DescendantsPageListModalLazyLoaded } from '~/client/components/DescendantsPageListModal';
@@ -153,7 +150,11 @@ const Page: NextPageWithLayout<Props> = (props: Props) => {
 
   // If the data on the page changes without router.push, pageWithMeta remains old because getServerSideProps() is not executed
   // So preferentially take page data from useSWRxCurrentPage
-  const pagePath = currentPagePath ?? props.currentPathname;
+  // Note: Memoize to prevent unnecessary re-renders of PageView
+  const pagePath = useMemo(
+    () => currentPagePath ?? props.currentPathname,
+    [currentPagePath, props.currentPathname],
+  );
 
   const title = useCustomTitleForPage(pagePath);