Skip to content

Commit

Permalink
feat: 페이지 이동될 때 스크롤 맨 위에 위치하도록 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
YesHyeon committed Aug 5, 2023
1 parent 0948912 commit bc78492
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/hooks/useScrollToTop.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { useEffect } from 'react';
import { useLocation } from 'react-router-dom';

export default function useScrollToTop() {
const { pathname } = useLocation();

useEffect(() => {
window.scrollTo(0, 0);
}, [pathname]);

return null;
}
5 changes: 5 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,22 @@ import theme from './styles/theme';
import { RecoilRoot } from 'recoil';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';

import useScrollToTop from './hooks/useScrollToTop';

const rootElement = document.getElementById('root');
if (!rootElement) throw new Error('Failed to find the root element');
const root = ReactDOM.createRoot(rootElement);

const queryClient = new QueryClient();

const ScrollToTop = useScrollToTop;

root.render(
<ThemeProvider theme={theme}>
<RecoilRoot>
<QueryClientProvider client={queryClient}>
<BrowserRouter>
<ScrollToTop />
<GlobalStyle />
<App />
</BrowserRouter>
Expand Down

0 comments on commit bc78492

Please sign in to comment.