Answers for "add event listener react hooks"

0

add event listener react hooks

import { useEffect } from "react"

export default function useEvent(event, handler, passive = false) {
  useEffect(() => {
    // initiate the event handler
    window.addEventListener(event, handler, passive)

    // this will clean up the event every time the component is re-rendered
    return function cleanup() {
      window.removeEventListener(event, handler)
    }
  })
}
Posted by: Guest on August-06-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language