fix: side effect of regex '/g'

This commit is contained in:
Paul Pan 2023-07-19 14:45:10 +08:00
parent 6c9ab90843
commit f858c91a12
2 changed files with 5 additions and 11 deletions

View File

@ -21,15 +21,9 @@ function Root() {
const nav = useNavigation(); const nav = useNavigation();
const location = useLocation(); const location = useLocation();
const [curTab, setCurTab] = useState("home"); const curTab = NavConfigs.filter((c) => c.regex.test(location.pathname))
.map((c) => c.key)
useEffect(() => { .concat("home")[0];
setCurTab(
NavConfigs.filter((c) => c.regex.test(location.pathname))
.map((c) => c.key)
.concat(["home"])[0],
);
}, [location]);
return ( return (
<> <>

View File

@ -26,13 +26,13 @@ const NavConfigs = [
key: "home", key: "home",
to: "/home", to: "/home",
label: "Home", label: "Home",
regex: /^\/$|^\/home$/gi, regex: /^\/$|^\/home$/i,
}, },
{ {
key: "problem", key: "problem",
to: "/search/", to: "/search/",
label: "Problem", label: "Problem",
regex: /^\/+(problem|search)($|\/.*)/gi, regex: /^\/+(problem|search)($|\/.*)/i,
}, },
]; ];