department-of-veterans-affairs/vets-website

View on GitHub
src/applications/mhv-medical-records/components/shared/ScrollToTop.jsx

Summary

Maintainability
A
2 hrs
Test Coverage
import React, { useEffect } from 'react';
import { useLocation } from 'react-router-dom';
import PropTypes from 'prop-types';

const ScrollToTop = props => {
  const location = useLocation();
  useEffect(
    () => {
      if (!location.hash) {
        window.scrollTo({ top: 0, left: 0, behavior: 'smooth' });
      }
    },
    [location],
  );

  return <>{props.children}</>;
};

ScrollToTop.propTypes = {
  children: PropTypes.any,
};

export default ScrollToTop;