Scroll to top when changing page (#493)

The scroll position was persisted when switching to a different page.
Thus, when the current page was scrolled down and the next page was also scrollable, the top of the new page was not visible
This commit is contained in:
schroda
2023-12-10 15:58:21 +01:00
committed by GitHub
parent c51897ed54
commit b6b902797e

View File

@@ -8,8 +8,8 @@
import { Container } from '@mui/material';
import CssBaseline from '@mui/material/CssBaseline';
import React from 'react';
import { Navigate, Route, Routes } from 'react-router-dom';
import React, { useLayoutEffect } from 'react';
import { Navigate, Route, Routes, useLocation } from 'react-router-dom';
import { loadErrorMessages, loadDevMessages } from '@apollo/client/dev';
import { __DEV__ } from '@apollo/client/utilities/globals';
import { AppContext } from '@/components/context/AppContext';
@@ -42,8 +42,20 @@ if (__DEV__) {
loadDevMessages();
loadErrorMessages();
}
const ScrollToTop = () => {
const { pathname } = useLocation();
useLayoutEffect(() => {
window.scrollTo(0, 0);
}, [pathname]);
return null;
};
export const App: React.FC = () => (
<AppContext>
<ScrollToTop />
<ServerUpdateChecker />
<CssBaseline />
<DefaultNavBar />