问题描述:Ant Design 的侧边栏菜单项在刷新页面后无法正确高亮当前选中的菜单项。
解决方法:
exact
属性。import { BrowserRouter as Router, Route, Link } from "react-router-dom";
import { Menu } from "antd";
const App = () => {
return (
);
};
const Home = () => Home
;
const About = () => About
;
export default App;
import { useState, useEffect } from "react";
import { useLocation } from "react-router-dom";
import { Menu } from "antd";
const App = () => {
const location = useLocation();
const [selectedKey, setSelectedKey] = useState("/");
useEffect(() => {
setSelectedKey(location.pathname);
}, [location]);
return (
);
};
通过以上两种方法,可以解决 Ant Design 侧边栏菜单项刷新后无法正确高亮当前选中的菜单项的问题。