Skip to content

Commit 23c5865

Browse files
authored
Merge pull request #283 from prgrms-web-devcourse-final-project/feat/282-scrollUp
[feat] 페이지 이동시 스크롤 위로 구현
2 parents b790de8 + 3834129 commit 23c5865

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/components/ScrollToTop.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { useEffect } from 'react';
2+
import { useLocation } from 'react-router';
3+
4+
function ScrollToTop() {
5+
const { pathname } = useLocation();
6+
7+
useEffect(() => {
8+
window.scrollTo({
9+
top: 0,
10+
behavior: 'smooth', // 부드럽게 스크롤 (즉시 이동은 'auto')
11+
});
12+
}, [pathname]);
13+
14+
return null;
15+
}
16+
17+
export default ScrollToTop;

src/main.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { registerSW } from 'virtual:pwa-register';
55
import { ReactQueryDevtools } from '@tanstack/react-query-devtools';
66
import './styles/index.css';
77
import App from './App.tsx';
8+
import ScrollToTop from '@/components/ScrollToTop.tsx';
89

910
// PWA 서비스 워커 등록
1011
const updateSW = registerSW({
@@ -24,6 +25,7 @@ const queryClient = new QueryClient();
2425
createRoot(document.getElementById('root')!).render(
2526
<QueryClientProvider client={queryClient}>
2627
<BrowserRouter>
28+
<ScrollToTop />
2729
<App />
2830
</BrowserRouter>
2931
<ReactQueryDevtools initialIsOpen={false} />

0 commit comments

Comments
 (0)