首页缓存

This commit is contained in:
禺狨
2023-07-03 10:21:00 +08:00
parent af136e1f57
commit a47ddcb471
4 changed files with 79 additions and 3 deletions

View File

@@ -1,3 +1,4 @@
export * from "./empty";
export * from "./footer";
export * from "./bar-footer";
export * from "./keep-alive";

View File

@@ -0,0 +1,29 @@
import { useUpdate } from "ahooks";
import { useEffect, useRef } from "react";
import { useLocation, useOutlet } from "react-router-dom";
function KeepAlive() {
const componentList = useRef(new Map());
const outLet = useOutlet();
const { pathname } = useLocation();
const forceUpdate = useUpdate();
useEffect(() => {
if (!componentList.current.has(pathname)) {
componentList.current.set(pathname, outLet);
}
forceUpdate();
}, [pathname]);
return (
<div>
{Array.from(componentList.current).map(([key, component]) => (
<div key={key} style={{ display: pathname === key ? "block" : "none" }}>
{component}
</div>
))}
</div>
);
}
export default KeepAlive;