How to use @react-hook/throttle - 5 common examples

To help you get started, we’ve selected a few @react-hook/throttle examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jaredLunde / react-hook / packages / window-size / src / throttled.js View on Github external
const useSizeHook = (dim, initialValue, opt) => {
  const {fps, leading} = opt
  const [size, setThrottledSize] = useThrottle(
    typeof document === 'undefined'
      ? initialValue
      : document.documentElement[dim],
    fps,
    leading
  )

  useEffect(() => {
    const setSize = () => setThrottledSize(document.documentElement[dim])
    window.addEventListener('resize', setSize)
    window.addEventListener('orientationchange', setSize)

    return () => {
      window.removeEventListener('resize', setSize)
      window.removeEventListener('orientationchange', setSize)
    }
github jaredLunde / react-hook / packages / window-size / src / throttled.ts View on Github external
const useSizeHook = (
  dim: Dimension,
  initialValue?: number,
  options: ThrottleOptions = emptyObj
): number => {
  const {fps, leading} = options
  const [size, setThrottledSize] = useThrottle(
    typeof document === 'undefined'
      ? initialValue
      : document.documentElement[dim],
    fps,
    leading
  )

  useEffect(() => {
    const setSize = (): void => setThrottledSize(document.documentElement[dim])
    window.addEventListener('resize', setSize)
    window.addEventListener('orientationchange', setSize)

    return (): void => {
      window.removeEventListener('resize', setSize)
      window.removeEventListener('orientationchange', setSize)
    }
github jaredLunde / react-hook / packages / mouse-position / src / index.js View on Github external
y: pageY - rect.top - window.pageYOffset,
        pageX,
        pageY,
        clientX,
        clientY,
        screenX,
        screenY,
        elementWidth: rect.width,
        elementHeight: rect.height,
        isOver: true
      })
    },
    emptyArr
  )

  const onMove = useThrottleCallback(_onMove, fps, true)
  const onEnter = useCallback(
    e => delay(
      enterDelay,
      () => {
        entered.current = true
        onMove(e)
      }
    ),
    [enterDelay, delay]
  )

  const onLeave = useCallback(
    () => delay(
      leaveDelay,
      () => {
        entered.current = false
github jaredLunde / react-hook / packages / mouse-position / src / index.ts View on Github external
pageX,
        pageY,
        clientX,
        clientY,
        screenX,
        screenY,
        elementWidth: rect.width,
        elementHeight: rect.height,
        isOver: true,
        isDown: prev.isDown,
      }))
    },
    [element]
  )

  const onMove = useThrottleCallback(onMove_, fps, true)

  useEffect((): void | (() => void) => {
    if (element !== null) {
      const onEnter = (e: MouseEvent): void => {
        delay(enterDelay, (): void => {
          entered.current = true
          onMove(e)
        })
      }
      const onLeave = (): void => {
        delay(leaveDelay, (): void => {
          entered.current = false
          setState(initialState)
        })
      }
      const onDown = (): void => {
github jaredLunde / react-hook / packages / window-scroll / src / index.ts View on Github external
export const useWindowScroll = (fps = 30): number => {
  const [scrollY, setThrottledScroll] = useThrottle(
    typeof window === 'undefined' ? 0 : getScrollY,
    fps,
    true
  )

  useEffect(() => {
    const handleScroll = (): void => setThrottledScroll(getScrollY())
    window.addEventListener('scroll', handleScroll)
    return (): void => {
      window.removeEventListener('scroll', handleScroll)
    }
  }, emptyArr)

  return scrollY
}

@react-hook/throttle

A React hook for throttling setState and other callbacks

MIT
Latest version published 4 years ago

Package Health Score

64 / 100
Full package analysis