1. import { useState } from 'react';
    2. interface ISingleAndDoubleState {
    3. delay?: number;
    4. click: (...args: any) => void;
    5. dbclick?: (...args: any) => void;
    6. }
    7. const useSingleAndDoubleClick = (state: ISingleAndDoubleState) => {
    8. const { delay = 250, click, dblclick } = state ?? {};
    9. const timerRef = useRef<number | null>(null);
    10. const countRef = useRef(0);
    11. const onClick = useCallback((...args) => {
    12. clearTimeout(timerRef.current);
    13. timerRef.current = setTimeout(() => {
    14. if (countRef.current === 1) {
    15. click?.(...args);
    16. }
    17. }, delay);
    18. }, [delay, click]);
    19. const onDoubleClick = useCallback((...args) => {
    20. clearTimeout(timerRef.current);
    21. dblclick?.(...args);
    22. }, [dblclick]);
    23. return { onClick, onDoubleClick } as const;
    24. }
    25. export default useSingleAndDoubleClick;