diff --git a/src/SiderMenu/BaseMenu.tsx b/src/SiderMenu/BaseMenu.tsx index bd33085f..d6bed7af 100644 --- a/src/SiderMenu/BaseMenu.tsx +++ b/src/SiderMenu/BaseMenu.tsx @@ -271,6 +271,7 @@ const BaseMenu: React.FC = (props) => { // 用于减少 defaultOpenKeys 计算的组件 const defaultOpenKeysRef = useRef([]); const [postMenuData, setPostMenuData] = useState(() => menuData); + const postMenuDataRef = useRef(postMenuData); const { pathname } = location; @@ -313,7 +314,10 @@ const BaseMenu: React.FC = (props) => { if (menu.defaultOpenAll || propsOpenKeys === false || flatMenuKeys.length) { return; } - const keys = getSelectedMenuKeys(location.pathname || '/', menuData || []); + const keys = getSelectedMenuKeys( + location.pathname || '/', + postMenuDataRef.current || [], + ); if (keys) { openKeysRef.current = keys; setOpenKeys(keys); @@ -332,7 +336,10 @@ const BaseMenu: React.FC = (props) => { useEffect(() => { // if pathname can't match, use the nearest parent's key - const keys = getSelectedMenuKeys(location.pathname || '/', menuData || []); + const keys = getSelectedMenuKeys( + location.pathname || '/', + postMenuDataRef.current || [], + ); const animationFrameId = requestAnimationFrame(() => { if (keys.join('-') !== (selectedKeys || []).join('-')) { setSelectedKeys(keys); @@ -364,6 +371,10 @@ const BaseMenu: React.FC = (props) => { const [menuUtils] = useState(() => new MenuUtil(props)); + /** + * 这里需要用 menuData + * 为了计算 splitMenus 需要用最全的 menuData + */ useEffect(() => { if (splitMenus && openKeys) { const keys = getSelectedMenuKeys( @@ -391,6 +402,15 @@ const BaseMenu: React.FC = (props) => { } } + const finallyData = props.postMenuData + ? props.postMenuData(postMenuData) + : postMenuData; + + /** + * 记下最新的 menuData + */ + postMenuDataRef.current = finallyData; + return ( = (props) => { onOpenChange={(keys) => setOpenKeys(keys as string[])} {...props.menuProps} > - {menuUtils.getNavMenuItems( - props.postMenuData ? props.postMenuData(postMenuData) : postMenuData, - false, - )} + {menuUtils.getNavMenuItems(finallyData, false)} ); }; diff --git a/tests/__tests__/__snapshots__/settingDrawer.test.tsx.snap b/tests/__tests__/__snapshots__/settingDrawer.test.tsx.snap index 8015e752..737f876f 100644 --- a/tests/__tests__/__snapshots__/settingDrawer.test.tsx.snap +++ b/tests/__tests__/__snapshots__/settingDrawer.test.tsx.snap @@ -72,40 +72,44 @@ exports[`settingDrawer.test base user 1`] = ` />

主题色

- - - + + +
@@ -671,6 +675,15 @@ exports[`settingDrawer.test hideColors = true 1`] = ` style="min-height:42px" /> +
+

+ 主题色 +

+

主题色

- - - + + +
@@ -1806,40 +1823,44 @@ exports[`settingDrawer.test hideHintAlert = true 1`] = ` />

主题色

- - - + + +
@@ -2372,40 +2393,44 @@ exports[`settingDrawer.test hideLoading = true 1`] = ` />

主题色

- - - + + +
@@ -2972,40 +2997,44 @@ exports[`settingDrawer.test settings = undefined 1`] = ` />

主题色

- - - + + +