feat: 路由改造
This commit is contained in:
27
src/components/ScrollToTop/index.js
Normal file
27
src/components/ScrollToTop/index.js
Normal file
@@ -0,0 +1,27 @@
|
||||
// src/components/ScrollToTop/index.js
|
||||
// 路由切换时自动滚动到页面顶部
|
||||
|
||||
import { useEffect } from 'react';
|
||||
import { useLocation } from 'react-router-dom';
|
||||
|
||||
/**
|
||||
* ScrollToTop - 路由切换时自动滚动到顶部
|
||||
*
|
||||
* 使用方式:在 App.js 的 Router 内部添加此组件
|
||||
*
|
||||
* @example
|
||||
* <BrowserRouter>
|
||||
* <ScrollToTop />
|
||||
* <Routes>...</Routes>
|
||||
* </BrowserRouter>
|
||||
*/
|
||||
export default function ScrollToTop() {
|
||||
const { pathname } = useLocation();
|
||||
|
||||
useEffect(() => {
|
||||
// 路径改变时滚动到顶部
|
||||
window.scrollTo(0, 0);
|
||||
}, [pathname]);
|
||||
|
||||
return null;
|
||||
}
|
||||
Reference in New Issue
Block a user