Skip to content

Instantly share code, notes, and snippets.

@miguel-ra
Last active September 27, 2020 23:38
Show Gist options
  • Save miguel-ra/4c2357c9ea570dbe61b15792c6ba2329 to your computer and use it in GitHub Desktop.
Save miguel-ra/4c2357c9ea570dbe61b15792c6ba2329 to your computer and use it in GitHub Desktop.
React hook to handle single click and double click on element
const DELAY = 250;
const useSingleAndDoubleClick = (onClick, onDoubleClick) => {
const clicks = useRef(0);
const callFunction = useCallback(
debounce(() => {
clicks.current === 3 ? onDoubleClick() : onClick();
clicks.current = 0;
}, DELAY),
[]
);
const handleClick = () => {
clicks.current++;
callFunction();
};
const handleDoubleClick = () => {
clicks.current++;
};
return { handleClick, handleDoubleClick };
};
export default useSingleAndDoubleClick;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment