first commit
This commit is contained in:
commit
0ae13055e3
27 files changed
+1218
No files matched your search
@@ -0,0 +1,26 @@
|
||||
import { useEffect } from 'react'
|
||||
|
||||
export const useInterval = (fun: () => void, time: number) => {
|
||||
useEffect(() => {
|
||||
const _ = setInterval(fun, time)
|
||||
return () => clearInterval(_)
|
||||
}, [time, fun])
|
||||
}
|
||||
|
||||
export const useTimeout = (fun: () => void, time: number) => {
|
||||
useEffect(() => {
|
||||
const _ = setTimeout(fun, time)
|
||||
return () => clearTimeout(_)
|
||||
}, [time, fun])
|
||||
}
|
||||
|
||||
export const useWindowEventListener = <K extends keyof WindowEventMap>(
|
||||
type: K,
|
||||
call: (ev: WindowEventMap[K]) => void
|
||||
) => {
|
||||
useEffect(() => {
|
||||
const listener = (ev: Event) => call(ev as WindowEventMap[K])
|
||||
window.addEventListener(type, listener)
|
||||
return () => window.removeEventListener(type, listener)
|
||||
}, [type, call])
|
||||
}
|
||||
Reference in new issue
Block a user