Snippets
useScrollPosition
To listen for the vertical scroll position of the browser window.
import { useEffect, useState } from "react"
 
const useScrollPosition = () => {
  const [scrollPosition, setScrollPosition] = useState(0)
 
  useEffect(() => {
    const updatePosition = () => {
      setScrollPosition(window.scrollY)
    }
    window.addEventListener("scroll", updatePosition)
    updatePosition()
    return () => window.removeEventListener("scroll", updatePosition)
  }, [])
 
  return scrollPosition
}
Last updated: Jun 2, 2024
Home
HomeProjectsThoughtsSnippetsDiscoveriesUses