From 15a68eef66d0c24dced77986647d702b916f1379 Mon Sep 17 00:00:00 2001 From: Zeke Zhang <958414905@qq.com> Date: Sun, 3 Nov 2024 19:34:35 +0800 Subject: [PATCH] perf(MenuEditor): prevent unnecessary re-renders --- .../CustomRouterContextProvider.tsx | 21 +++++++------------ 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/packages/core/client/src/application/CustomRouterContextProvider.tsx b/packages/core/client/src/application/CustomRouterContextProvider.tsx index 623b7b5fa7..279e3ab412 100644 --- a/packages/core/client/src/application/CustomRouterContextProvider.tsx +++ b/packages/core/client/src/application/CustomRouterContextProvider.tsx @@ -8,16 +8,7 @@ */ import React, { FC, useEffect } from 'react'; -import { - Location, - NavigateFunction, - NavigateOptions, - PathMatch, - useLocation, - useMatch, - useNavigate, - useParams, -} from 'react-router-dom'; +import { Location, NavigateFunction, NavigateOptions, useLocation, useNavigate, useParams } from 'react-router-dom'; const NavigateNoUpdateContext = React.createContext(null); NavigateNoUpdateContext.displayName = 'NavigateNoUpdateContext'; @@ -34,10 +25,10 @@ IsAdminPageContext.displayName = 'IsAdminPageContext'; const CurrentPageUidContext = React.createContext(''); CurrentPageUidContext.displayName = 'CurrentPageUidContext'; -const MatchAdminContext = React.createContext | null>(null); +const MatchAdminContext = React.createContext(false); MatchAdminContext.displayName = 'MatchAdminContext'; -const MatchAdminNameContext = React.createContext | null>(null); +const MatchAdminNameContext = React.createContext(false); MatchAdminNameContext.displayName = 'MatchAdminNameContext'; const IsInSettingsPageContext = React.createContext(false); @@ -49,12 +40,14 @@ const IsInSettingsPageProvider: FC = ({ children }) => { }; const MatchAdminProvider: FC = ({ children }) => { - const matchAdmin = useMatch('/admin'); + const location = useLocation(); + const matchAdmin = location.pathname.startsWith('/admin'); return {children}; }; const MatchAdminNameProvider: FC = ({ children }) => { - const matchAdminName = useMatch('/admin/:name'); + const location = useLocation(); + const matchAdminName = /^\/admin\/.+/.test(location.pathname); return {children}; };