forked from tanner/qotnews
refactor: Convert ScrollToTop to functional component with hooks
This commit is contained in:
@@ -1,14 +1,10 @@
|
|||||||
import React from 'react';
|
import { useEffect } from 'react';
|
||||||
import { withRouter } from 'react-router-dom';
|
import { useLocation } from 'react-router-dom';
|
||||||
|
|
||||||
class ScrollToTop extends React.Component {
|
function ScrollToTop() {
|
||||||
componentDidUpdate(prevProps) {
|
const { pathname } = useLocation();
|
||||||
//console.log(this.props.location.pathname, prevProps.location.pathname);
|
|
||||||
|
|
||||||
if (this.props.location.pathname === prevProps.location.pathname) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
if (localStorage.getItem('scrollLock') === 'True') {
|
if (localStorage.getItem('scrollLock') === 'True') {
|
||||||
localStorage.setItem('scrollLock', 'False');
|
localStorage.setItem('scrollLock', 'False');
|
||||||
return;
|
return;
|
||||||
@@ -16,11 +12,9 @@ class ScrollToTop extends React.Component {
|
|||||||
|
|
||||||
window.scrollTo(0, 0);
|
window.scrollTo(0, 0);
|
||||||
document.body.scrollTop = 0;
|
document.body.scrollTop = 0;
|
||||||
}
|
}, [pathname]);
|
||||||
|
|
||||||
render() {
|
|
||||||
return null;
|
return null;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default withRouter(ScrollToTop);
|
export default ScrollToTop;
|
||||||
|
|||||||
Reference in New Issue
Block a user