自动滚到到顶部

This commit is contained in:
禺狨 2023-04-17 11:19:15 +08:00
parent 23d16b52dd
commit 36a24ae87f
2 changed files with 16 additions and 1 deletions

12
src/AutoTop.ts Normal file
View File

@ -0,0 +1,12 @@
import { useLayoutEffect } from "react";
import { useLocation } from "react-router-dom";
const AutoScorllTop = ({ children }) => {
const location = useLocation();
useLayoutEffect(() => {
document.documentElement.scrollTo(0, 0);
}, [location.pathname]);
return children;
};
export default AutoScorllTop;

View File

@ -7,6 +7,7 @@ import { Provider } from "react-redux";
import store from "./store";
import { ConfigProvider } from "antd";
import zhCN from "antd/locale/zh_CN";
import AutoScorllTop from "./AutoTop";
ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
<Provider store={store}>
@ -15,7 +16,9 @@ ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
theme={{ token: { colorPrimary: "#ff4d4f" } }}
>
<BrowserRouter>
<App />
<AutoScorllTop>
<App />
</AutoScorllTop>
</BrowserRouter>
</ConfigProvider>
</Provider>