📌  相关文章
📜  添加事件侦听器反应挂钩 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:03:07.798000             🧑  作者: Mango

代码示例1
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)
    }
  })
}