Handle double click events in React
To Handle double click events in React:
- Pass a click event handler function into onClick prop of the element.
- Inside the event handler, check the click count by checking the detail property of the event object.
- Add condition for detail is equal to 2.
The below code example shows how to handle double click events in React element.
import React from 'react';
export default function App() {
const clickHandler = (event) => {
if(event.detail == 2){
console.log("Double Clicked")
}
}
return (
<>
<button onClick={clickHandler}>Click</button>
</>
);
}
Here we passed a click event handler clickHandler
into the onClick prop of a button. Inside the clickHandler
function, we have an event as an argument that has detail as a property. The detail property holds the value of number of times the user clicked on the button in a small interval of time.
To check the click count, we have added if condition like:
if(event.detail == 2){
console.log("Double Clicked")
}
If we want to handle triple click, we can check detail property is equal to 3 as given below.
if(event.detail == 3){
console.log("Triple Clicked")
}
The event objects contains so much other data related to the click event like:
- The
pagex
andpagey
coordinates determine where the user clicked the button - Whether the shift key or alt key is pressed or not at the time of click
- Timestamp of the button click etc.
Read more about event handling in React doc.
Use Native onDoubleClick() event
We have a native onDoubleClick()
event for handling the double click in React. The below code shows how to use onDoubleClick()
in React.
import React from 'react';
export default function App() {
const doubleClickHandler = (event) => {
console.log("double click");
}
return (
<>
<button onDoubleClick={doubleClickHandler}>Click</button>
</>
);
}
To handle double click we passed doubleClickHandler into the onDoubleClick
prop of the button. So when the user clicks twice on the button, doubleClickHandler
function will get invoked.
This approach has a major drawback which can be explained in the below code.
import React from 'react';
export default function App() {
const singleClickHandler = (event) => {
console.log("single click");
}
const doubleClickHandler = (event) => {
console.log("double click");
}
return (
<>
<button onClick={singleClickHandler} onDoubleClick={doubleClickHandler}>Click</button>
</>
);
}
The output in the console when a user double clicked on the button is given below.
single click
single click
double click
You may notice that singleClickHandler
function was invoked twice before invoking the doubleClickHandler
function. This effect isn't desirable when we have a single click and a double click have different functions. So this approach is not suitable if we have separate functionality for single click and double click of elements. In such cases, we opt for the first solution that checks the detail property of event we discussed in this article.
Conclusion
To handle the double click events in React, pass an onClick event handler function into the element. Inside the function check the detail property of the event object to determine the number of counts the user clicked on the element.
On datainfinities.com, Read articles all around JavaScript, React, Node.js, PHP, Laravel, and Shopify.
Related Blogs
Reset to the initial state in React
Adjacent JSX elements must be wrapped in an enclosing tag
Import and export may only appear at the top level
How to use Profiler API in React to measure render performance
Invalid hook call. Hooks can only be called inside the body of a function component
Unexpected default export of anonymous function
How to get window width and height in React
Create a Back button with React Router
Explore All Blogs