Skip to content

Instantly share code, notes, and snippets.

@rayliao
Created June 3, 2022 12:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rayliao/6709c9aef02e12194c966a1dee75a2b9 to your computer and use it in GitHub Desktop.
Save rayliao/6709c9aef02e12194c966a1dee75a2b9 to your computer and use it in GitHub Desktop.
React Skills
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