From d1c6e94edff0565bca0ed5fd5cb5be5bb9dfb194 Mon Sep 17 00:00:00 2001 From: ZLY Date: Thu, 4 Sep 2025 09:50:57 +0800 Subject: [PATCH] =?UTF-8?q?pref(layout):=20=E4=BD=BF=E7=94=A8useMemo?= =?UTF-8?q?=E5=AF=B9=E8=B7=AF=E7=94=B1=E8=A1=A8=E8=BF=9B=E8=A1=8C=E9=A2=84?= =?UTF-8?q?=E6=9E=84=E5=BB=BA=EF=BC=8C=E9=81=BF=E5=85=8D=E5=9B=A0=E5=BC=82?= =?UTF-8?q?=E6=AD=A5=E6=95=B0=E6=8D=AE=E5=A4=84=E7=90=86=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E7=9A=84=E9=97=AA=E7=83=81=E9=97=AE=E9=A2=98(=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2=E5=85=88=E8=B7=B3=E8=BD=AC403=E5=90=8E=E6=B8=B2?= =?UTF-8?q?=E6=9F=93=E6=AD=A3=E5=B8=B8)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/layout.tsx | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/pages/layout.tsx b/src/pages/layout.tsx index 6624209..7db0797 100644 --- a/src/pages/layout.tsx +++ b/src/pages/layout.tsx @@ -1,4 +1,4 @@ -import React, { useState, ReactNode, useRef, useEffect } from 'react'; +import React, { useState, ReactNode, useRef, useEffect, useMemo } from 'react'; import { Layout, Menu, Breadcrumb, Spin } from '@arco-design/web-react'; import cs from 'classnames'; import { @@ -114,14 +114,14 @@ function PageLayout({ children }: { children: ReactNode }) { const paddingTop = showNavbar ? { paddingTop: navbarHeight } : {}; const paddingStyle = { ...paddingLeft, ...paddingTop }; - // 初始化时创建并实例路由映射 - useEffect(() => { - // 仅构建 routeMap,不渲染菜单 + const routeMapMemo = useMemo(() => { + const map = new Map(); + function buildRouteMap(_routes: IRoute[], parentNode = []) { _routes.forEach((route) => { const { breadcrumb = true, ignore } = route; - routeMap.current.set( + map.set( `/${route.key}`, breadcrumb ? [...parentNode, route.name] : [] ); @@ -129,7 +129,7 @@ function PageLayout({ children }: { children: ReactNode }) { const visibleChildren = (route.children || []).filter((child) => { const { ignore, breadcrumb = true } = child; if (ignore || route.ignore) { - routeMap.current.set( + map.set( `/${child.key}`, breadcrumb ? [...parentNode, route.name, child.name] : [] ); @@ -144,6 +144,7 @@ function PageLayout({ children }: { children: ReactNode }) { } buildRouteMap(routes); + return map; }, [routes]); function renderRoutes(locale) { @@ -217,7 +218,7 @@ function PageLayout({ children }: { children: ReactNode }) { } useEffect(() => { - const routeConfig = routeMap.current.get(pathname); + const routeConfig = routeMapMemo.get(pathname); setBreadCrumb(routeConfig || []); updateMenuStatus(); }, [pathname]); @@ -279,7 +280,7 @@ function PageLayout({ children }: { children: ReactNode }) { )} - {routeMap.current.has(pathname) ? children : } + {routeMapMemo.has(pathname) ? children : } {/*{showFooter &&