Skip to content

Commit 4ddc170

Browse files
committed
feat: Add scroll-to-top.tsx
1 parent 1cdf0e3 commit 4ddc170

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

src/routes.tsx

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { BrowserRouter, Routes as ReactRoutes, Route, useLocation } from 'react-router';
1+
import { BrowserRouter, Routes as ReactRoutes, Route } from 'react-router';
22

33
import { AppContainer } from '@/layout/app-container';
44
import { AuthLayout } from '@/layout/auth-layout';
@@ -8,7 +8,7 @@ import { NotFoundPage } from '@/pages/not-found-page';
88
import { LoginPage } from '@/pages/auth/login-page';
99
import { LandingPage } from '@/pages/landing-page';
1010
import { MainPage } from '@/pages/main/main-page';
11-
import { useEffect } from 'react';
11+
import { ScrollToTop } from '@/shared/helpers';
1212

1313
const ROUTES_DATA = [
1414
{
@@ -19,14 +19,9 @@ const ROUTES_DATA = [
1919
] as const;
2020

2121
export function Routes() {
22-
const { pathname } = useLocation();
23-
24-
useEffect(() => {
25-
window.scrollTo(0, 0);
26-
}, [pathname]);
27-
2822
return (
2923
<BrowserRouter>
24+
<ScrollToTop />
3025
<ReactRoutes>
3126
<Route index path={APP_ROUTES.landing} element={<LandingPage />} />
3227

src/shared/helpers/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from './scroll-to-top';
12
export * from './get-share-url';
23
export * from './animator';
34
export * from './common';
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { useLocation } from 'react-router';
2+
import { useEffect } from 'react';
3+
4+
export function ScrollToTop() {
5+
const { pathname } = useLocation();
6+
7+
useEffect(() => {
8+
window.scrollTo(0, 0);
9+
}, [pathname]);
10+
11+
return null;
12+
}

0 commit comments

Comments
 (0)