onClick works but onDoubleClick is ignored on React component

后端 未结 6 496
轻奢々
轻奢々 2020-12-05 03:38

I am building a Minesweeper game with React and want to perform a different action when a cell is single or double clicked. Currently, the onDoubleClick functi

6条回答
  •  误落风尘
    2020-12-05 04:30

    This is the solution of a like button with increment and discernment values based on solution of Erminea.

    useEffect(() => {
        let singleClickTimer;
        if (clicks === 1) {
          singleClickTimer = setTimeout(
            () => {
              handleClick();
              setClicks(0);
            }, 250);
        } else if (clicks === 2) {
          handleDoubleClick();
          setClicks(0);
        }
        return () => clearTimeout(singleClickTimer);
      }, [clicks]);
    
      const handleClick = () => {
        console.log('single click');
        total = totalClicks + 1;
        setTotalClicks(total);
      }
    
      const handleDoubleClick = () => {
        console.log('double click');
        if (total > 0) {
          total = totalClicks - 1;
        }
        setTotalClicks(total);
      }
    
      return (
        
    setClicks(clicks + 1)} > Likes | {totalClicks}
    )

提交回复
热议问题