Skip to content

Instantly share code, notes, and snippets.

@DipanshKhandelwal
Created February 4, 2021 13:18
Show Gist options
  • Save DipanshKhandelwal/8c3625f27684e35bf645910acf30915d to your computer and use it in GitHub Desktop.
Save DipanshKhandelwal/8c3625f27684e35bf645910acf30915d to your computer and use it in GitHub Desktop.
React hook to scroll to anchor tags
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