Skip to content

Commit

Permalink
add: helpers for scrolltotop use react-router-dom
Browse files Browse the repository at this point in the history
  • Loading branch information
hudaputrasantosa committed Mar 7, 2024
1 parent 3f68c6b commit db48b85
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
13 changes: 13 additions & 0 deletions src/helpers/ScrollToTop.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { useLayoutEffect } from "react";
import { useLocation } from "react-router-dom";

const ScrollToTop = ({ children }) => {
const { pathname } = useLocation();

useLayoutEffect(() => {
document.documentElement.scrollTo(0, 0);
}, [pathname]);
return children;
};

export default ScrollToTop;
21 changes: 12 additions & 9 deletions src/routes/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,22 @@ import Blogs from "../pages/Blogs";
import Contact from "../pages/Contact";
import NotFound from "../pages/NotFound";
import Certification from "../pages/Certification";
import ScrollToTop from "../helpers/ScrollToTop";

const Router = () => {
return (
<BrowserRouter>
<Routes>
<Route path="/" element={<About />} />
<Route path="/skills" element={<Skills />} />
<Route path="/projects" element={<Projects />} />
<Route path="/certification" element={<Certification />} />
<Route path="/blogs" element={<Blogs />} />
<Route path="/contact" element={<Contact />} />
<Route path="*" element={<NotFound />} />
</Routes>
<ScrollToTop>
<Routes>
<Route path="/" element={<About />} />
<Route path="/skills" element={<Skills />} />
<Route path="/projects" element={<Projects />} />
<Route path="/certification" element={<Certification />} />
<Route path="/blogs" element={<Blogs />} />
<Route path="/contact" element={<Contact />} />
<Route path="*" element={<NotFound />} />
</Routes>
</ScrollToTop>
</BrowserRouter>
);
};
Expand Down

0 comments on commit db48b85

Please sign in to comment.