Last active
September 27, 2020 23:38
-
-
Save miguel-ra/4c2357c9ea570dbe61b15792c6ba2329 to your computer and use it in GitHub Desktop.
React hook to handle single click and double click on element
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
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