Skip to content

Commit

Permalink
scroll to top on URL change
Browse files Browse the repository at this point in the history
  • Loading branch information
haddercone committed Aug 1, 2023
1 parent ef932e7 commit bd202f1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/components/AppLayout.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,25 @@ import { useState } from "react";
import UserContext from "../utils/UserContext";
import { Provider } from "react-redux";
import store from "../utils/store";

import ScrollToTop from "./ScrollToTop";
const AppLayout = () => {
const [user, setUser] = useState({
name: "Robin Singh",
email: "44robin.rs@gmail.com",
});

return (
<>
<ScrollToTop />
<Provider store={store}>
<UserContext.Provider value={{ user: user, setUser: setUser }}>

<Head />
<Outlet />
<Footer />
</UserContext.Provider>
</Provider>
</>
);
};

Expand Down
12 changes: 12 additions & 0 deletions src/components/ScrollToTop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { useLocation } from "react-router-dom";
import { useEffect } from "react";

const ScrollToTop = () => {
const { pathname } = useLocation();
useEffect(() => {
window.scrollTo(0, 0);
}, [pathname]);
return null;
};

export default ScrollToTop;

0 comments on commit bd202f1

Please sign in to comment.