useThrottledEventHandler

    API

    import useThrottledEventHandler from '@restart/hooks/useThrottledEventHandler'
    • useThrottledEventHandler<TEvent>(handler: (event: TEvent) => void) => ThrottledHandler<TEvent>

      Creates a event handler function throttled by requestAnimationFrame that returns the most recent event. Useful for noisy events that update react state.

      function Component() {
      const [position, setPosition] = useState();
      const handleMove = useThrottledEventHandler<React.PointerEvent>(
      (event) => {
      setPosition({
      top: event.clientX,
      left: event.clientY,
      })
      }
      )
      return (
      <div onPointerMove={handleMove}>
      <div style={position} />
      </div>
      );
      }

      Type Parameters

      • TEventTEvent

        The event object passed to the handler function

      Parameters

      • handler(event: TEvent) => void

        An event handler function

      Return Value ThrottledHandler<TEvent>

      The event handler with a clear method attached for clearing any in-flight handler calls