scrollToTop.tsx 643 B

123456789101112131415161718192021222324252627
  1. import {Component} from 'react';
  2. import {Location} from 'history';
  3. import {callIfFunction} from 'sentry/utils/callIfFunction';
  4. type Props = {
  5. location: Location;
  6. disable: (location: Location, prevLocation: Location) => boolean;
  7. };
  8. class ScrollToTop extends Component<Props> {
  9. componentDidUpdate(prevProps: Props) {
  10. const {disable, location} = this.props;
  11. const shouldDisable = callIfFunction(disable, location, prevProps.location);
  12. if (!shouldDisable && this.props.location !== prevProps.location) {
  13. window.scrollTo(0, 0);
  14. }
  15. }
  16. render() {
  17. return this.props.children;
  18. }
  19. }
  20. export default ScrollToTop;