From 6e9717f8d3c3c98cd9e8504dca088c258dac4616 Mon Sep 17 00:00:00 2001 From: frikky Date: Thu, 18 Feb 2021 08:00:31 +0100 Subject: [PATCH] Added frontend autoscroll --- frontend/src/components/ScrollToTop.jsx | 32 +++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 frontend/src/components/ScrollToTop.jsx diff --git a/frontend/src/components/ScrollToTop.jsx b/frontend/src/components/ScrollToTop.jsx new file mode 100644 index 00000000..6b3c9911 --- /dev/null +++ b/frontend/src/components/ScrollToTop.jsx @@ -0,0 +1,32 @@ +import { useEffect } from 'react'; +import { withRouter } from 'react-router-dom'; +import ReactGA from 'react-ga'; + +function ScrollToTop({setCurpath, history }) { + useEffect(() => { + const unlisten = history.listen(() => { + window.scroll({ + top: 0, + left: 0, + behavior: "smooth", + }); + + //ReactGA.event({ + // category: "referral", + // action: "new_user_referral", + // label: "", + //}) + + ReactGA.pageview(window.location.pathname) + setCurpath(window.location.pathname) + }); + return () => { + unlisten(); + } + }, []); + + return (null); +} + +// https://stackoverflow.com/questions/36904185/react-router-scroll-to-top-on-every-transition +export default withRouter(ScrollToTop);