mirror of
https://github.com/zhigang1992/react.git
synced 2026-01-27 08:58:46 +08:00
feat: add click any where hooks
This commit is contained in:
14
components/utils/use-click-anywhere.ts
Normal file
14
components/utils/use-click-anywhere.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { useEffect } from 'react'
|
||||
|
||||
const useClickAnyWhere = (
|
||||
handler: (event: Event) => void,
|
||||
) => {
|
||||
useEffect(() => {
|
||||
const callback = (event: Event) => handler(event)
|
||||
|
||||
document.addEventListener('click', callback)
|
||||
return () => document.removeEventListener('click', callback)
|
||||
}, [handler])
|
||||
}
|
||||
|
||||
export default useClickAnyWhere
|
||||
Reference in New Issue
Block a user