Created
February 4, 2021 13:18
-
-
Save DipanshKhandelwal/8c3625f27684e35bf645910acf30915d to your computer and use it in GitHub Desktop.
React hook to scroll to anchor tags
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useEffect } from 'react' | |
const useAnchorScroll = (dependencies: any[]) => { | |
useEffect(() => { | |
if (location.hash) { | |
const anchorEl = document.getElementById(location.hash.slice(1)) | |
if (anchorEl) { | |
anchorEl.scrollIntoView({ | |
behavior: 'smooth', | |
block: 'start' | |
}) | |
} | |
} | |
}, dependencies) | |
} | |
export default useAnchorScroll |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment