Skip to content

Instantly share code, notes, and snippets.

@watadarkstar
Created July 12, 2019 14:00
Show Gist options
  • Save watadarkstar/8c626c3c271e26f6ca6cdacac9cd9fb9 to your computer and use it in GitHub Desktop.
Save watadarkstar/8c626c3c271e26f6ca6cdacac9cd9fb9 to your computer and use it in GitHub Desktop.
import React, { useEffect } from 'react';
import PropTypes from 'prop-types';
function withScrollToTop(Component) {
function Wrapped({ location, ...rest }) {
const { pathname } = location;
useEffect(() => {
window.scrollTo(0, 0);
}, [pathname]);
return <Component location={location} {...rest} />;
}
Wrapped.propTypes = {
location: PropTypes.object.isRequired,
};
return Wrapped;
}
export default withScrollToTop;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment