Skip to content
Advertisement

onClick works but onDoubleClick is ignored on React component

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 function will never fire, the alert from onClick is shown. If I remove the onClick handler, onDoubleClick works. Why don’t both events work? Is it possible to have both events on an element?

JavaScript

Advertisement

Answer

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).

JavaScript

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”.

JavaScript

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.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement