Created
June 3, 2022 12:40
-
-
Save rayliao/6709c9aef02e12194c966a1dee75a2b9 to your computer and use it in GitHub Desktop.
React Skills
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useEffect } from "react"; | |
export const useOutsideClick = ( | |
ref: React.RefObject<HTMLElement>, | |
callback: () => void | |
) => { | |
useEffect(() => { | |
const handleClick = (e: MouseEvent) => { | |
if (ref.current && !ref.current.contains(e.target as Node)) { | |
callback(); | |
} | |
}; | |
document.addEventListener("click", handleClick); | |
return () => { | |
document.removeEventListener("click", handleClick); | |
}; | |
}, [ref, callback]); | |
}; | |
/** | |
* 格式化数字显示 | |
*/ | |
export const formatNumber = (num: number, gap: number = 3) => { | |
const reg = new RegExp(`\\B(?=(\\d{${gap}})+(?!\\d))`, "g"); | |
return num.toString().replace(reg, " "); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment