useGlobalListener

    API

    import useGlobalListener from '@restart/hooks/useGlobalListener'
    • useGlobalListener<K>(event: K, handler: DocumentEventHandler<K>, capture: boolean | AddEventListenerOptions) => void

      Attaches an event handler outside directly to the document, bypassing the react synthetic event system.

      useGlobalListener('keydown', (event) => {
      console.log(event.key)
      })

      Parameters

      • eventK

        The DOM event name

      • handler(this: Document, ev: DocumentEventMap[K]) => any

        An event handler

      • captureboolean | AddEventListenerOptions

        Whether or not to listen during the capture event phase

      Return Value void