Skip to content

Commit

Permalink
Scroll to top
Browse files Browse the repository at this point in the history
  • Loading branch information
Souviksamanta34 committed Oct 6, 2024
1 parent 17f30a1 commit 69da97e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { useEffect } from "react";
import ScrollToTop from './ScrollToTop';
import { useStateValue } from "./StateProvider";
import { loadStripe } from "@stripe/stripe-js";
import { Elements } from "@stripe/react-stripe-js";
Expand Down Expand Up @@ -60,6 +61,7 @@ function App() {
return (
<StateProvider initialState={initialState} reducer={reducer}>
<Router>
<ScrollToTop />
<div className="app">
<Header />
<Routes>
Expand Down
14 changes: 14 additions & 0 deletions src/ScrollToTop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { useEffect } from 'react';
import { useLocation } from 'react-router-dom';

function ScrollToTop() {
const { pathname } = useLocation();

useEffect(() => {
window.scrollTo(0, 0);
}, [pathname]); // Scroll to top whenever the route changes

return null;
}

export default ScrollToTop;

0 comments on commit 69da97e

Please sign in to comment.