Skip to content

Instantly share code, notes, and snippets.

@ericdcobb
Created April 1, 2021 17:16
Show Gist options
  • Save ericdcobb/b9dc0e9d985c8bd0d9a3ecf9ec87d417 to your computer and use it in GitHub Desktop.
Save ericdcobb/b9dc0e9d985c8bd0d9a3ecf9ec87d417 to your computer and use it in GitHub Desktop.
const scrollToLocation = () => {
const scrolledRef = React.useRef(false);
const { hash } = useLocation();
React.useEffect(() => {
if (hash && !scrolledRef.current) {
const id = hash.replace('#', '');
const element = document.getElementById(id);
if (element) {
element.scrollIntoView({ behavior: 'smooth' });
scrolledRef.current = true;
}
}
});
};
@yelnyafacee
Copy link

hi, how can I adapt this to work with Next-js? need to use it with intersection observer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment