onClick works but onDoubleClick is ignored on React component

后端 未结 6 477
轻奢々
轻奢々 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:16

    you can use a custom hook to handle simple click and double click like this :

    function useSimpleAndDoubleClick(actionSimpleClick, actionDoubleClick, delay = 250) {
        const [click, setClick] = useState(0);
    
        useEffect(() => {
            const timer = setTimeout(() => {
                // simple click
                if (click === 1) actionSimpleClick();
                setClick(0);
            }, 300);
    
            // click in a delay < 250 = double click detected
            if (click === 2) actionDoubleClick();
    
            return () => {
                clearTimeout(timer);
            }
        }, [click]);
    
        return () => {
            setClick(prev => prev + 1);
        };
    }
    

    then in your component you can use :

    const click = useSimpleAndDoubleClick(callbackClick, callbackDoubleClick);
    <button onClick={click}>clic</button>
    
    0 讨论(0)
  • 2020-12-05 04:17

    The required result can be achieved by providing a very slight delay on firing off the normal click action, which will be cancelled when the double click event will happen.

      let timer = 0;
      let delay = 200;
      let prevent = false;
    
      doClickAction() {
        console.log(' click');
      }
      doDoubleClickAction() {
        console.log('Double Click')
      }
      handleClick() {
        let me = this;
        timer = setTimeout(function() {
          if (!prevent) {
            me.doClickAction();
          }
          prevent = false;
        }, delay);
      }
      handleDoubleClick(){
        clearTimeout(timer);
        prevent = true;
        this.doDoubleClickAction();
      }
     < button onClick={this.handleClick.bind(this)} 
        onDoubleClick = {this.handleDoubleClick.bind(this)} > click me </button>
    
    0 讨论(0)
  • 2020-12-05 04:28

    This is not a limitation of React, it is a limitation of the DOM's click and dblclick events. As suggested by Quirksmode's click documentation:

    Don't register click and dblclick events on the same element: it's impossible to distinguish single-click events from click events that lead to a dblclick event.

    For more current documentation, the W3C spec on the dblclick event states:

    A user agent must dispatch this event when the primary button of a pointing device is clicked twice over an element.

    A double click event necessarily happens after two click events.

    Edit:

    One more suggested read is jQuery's dblclick handler:

    It is inadvisable to bind handlers to both the click and dblclick events for the same element. The sequence of events triggered varies from browser to browser, with some receiving two click events before the dblclick and others only one. Double-click sensitivity (maximum time between clicks that is detected as a double click) can vary by operating system and browser, and is often user-configurable.

    0 讨论(0)
  • 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 (
        <div
          className="likeButton"
          onClick={() => setClicks(clicks + 1)}
        >
          Likes | {totalClicks}
        </div>
    )
    
    0 讨论(0)
  • 2020-12-05 04:31

    Edit:

    I've found that this is not an issue with React 0.15.3.


    Original:

    For React 0.13.3, here are two solutions.

    1. ref callback

    Note, even in the case of double-click, the single-click handler will be called twice (once for each click).

    const ListItem = React.createClass({
    
      handleClick() {
        console.log('single click');
      },
    
      handleDoubleClick() {
        console.log('double click');
      },
    
      refCallback(item) {
        if (item) {
          item.getDOMNode().ondblclick = this.handleDoubleClick;
        }
      },
    
      render() {
        return (
          <div onClick={this.handleClick}
               ref={this.refCallback}>
          </div>
        );
      }
    });
    
    module.exports = ListItem;
    

    2. lodash debounce

    I had another solution that used lodash, but I abandoned it because of the complexity. The benefit of this was that "click" was only called once, and not at all in the case of "double-click".

    import _ from 'lodash'
    
    const ListItem = React.createClass({
    
      handleClick(e) {
        if (!this._delayedClick) {
          this._delayedClick = _.debounce(this.doClick, 500);
        }
        if (this.clickedOnce) {
          this._delayedClick.cancel();
          this.clickedOnce = false;
          console.log('double click');
        } else {
          this._delayedClick(e);
          this.clickedOnce = true;
        }
      },
    
      doClick(e) {
        this.clickedOnce = undefined;
        console.log('single click');
      },
    
      render() {
        return (
          <div onClick={this.handleClick}>
          </div>
        );
      }
    });
    
    module.exports = ListItem;
    

    on the soapbox

    I appreciate the idea that double-click isn't something easily detected, but for better or worse it IS a paradigm that exists and one that users understand because of its prevalence in operating systems. Furthermore, it's a paradigm that modern browsers still support. Until such time that it is removed from the DOM specifications, my opinion is that React should support a functioning onDoubleClick prop alongside onClick. It's unfortunate that it seems they do not.

    0 讨论(0)
  • 2020-12-05 04:32

    Here's what I have done. Any suggestions for improvement are welcome.

    class DoubleClick extends React.Component {
      state = {counter: 0}
    
      handleClick = () => {
       this.setState(state => ({
        counter: this.state.counter + 1,
      }))
     }
    
    
      handleDoubleClick = () => {
       this.setState(state => ({
        counter: this.state.counter - 2,
      }))
     }
    
     render() {
       return(
       <>
        <button onClick={this.handleClick} onDoubleClick={this.handleDoubleClick>
          {this.state.counter}
        </button>
       </>
      )
     }
    }
    
    0 讨论(0)
提交回复
热议问题