React onDoubleClick Event
Last Updated :
19 Mar, 2025
The onDoubleClick event in React is a native DOM event that triggers when the user double-clicks on an element, typically using the left mouse button. This event is part of a group of mouse events that React handles, including onClick, onMouseDown, onMouseUp, and others.
- onDoubleClick occurs when a mouse button is pressed twice rapidly on the same element.
- The event will fire after two consecutive clicks within a short time frame, usually around 300 milliseconds. This makes it ideal for scenarios where you need to differentiate between a single click (which would use onClick) and a double-click (which requires onDoubleClick).
Syntax
<Element onDoubleClick={onDoubleClickHandler} />
- <Element>: The React component or HTML element (like <button>, <div>, etc.) you want to track double-clicks for.
- onDoubleClickHandler: A callback function that will be invoked when the onDoubleClick event is triggered.
It is similar to the HTML DOM ondblclick event but uses the camelCase convention in React.
Handling the onDoubleClick Event
The onDoubleClick event in React is used to handle specific actions or UI changes triggered by a double-click. You can use it to update state, trigger animations, or log messages.
CSS
/* App.css */
.App {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
body {
background-color: antiquewhite;
}
.App>h2 {
text-align: center;
}
.App>button {
width: 17rem;
font-size: larger;
padding: 2vmax auto;
height: 2.6rem;
color: white;
background-color: rgb(34, 34, 33);
border-radius: 10px;
}
button:hover {
background-color: rgb(80, 80, 78);
}
JavaScript
import React, { useState } from "react";
import "./App.css";
function App() {
const onDoubleClickHandler = () => {
console.log("You have Clicked Twice");
};
return (
<div className="App">
<h1> Hey Geek!</h1>
<button onDoubleClick={onDoubleClickHandler}>
Double Click Me!
</button>
</div>
);
}
export default App;
Output
OutponDoubleClick EventutIn this example
- This React component renders a button that logs "You have Clicked Twice" to the console when double-clicked.
- The onDoubleClickHandler function is triggered on the onDoubleClick event, which is bound to the button element.
- The message is logged when the user double-clicks the button.
Accessing the Event Object
The onDoubleClick event handler receives an event object that contains useful information about the event. This object can be used to get details about the mouse position, the target element, and other event properties.
JavaScript
import React, { useState } from "react";
function AccessEventObjectComponent() {
const [num, setNum] = useState(0);
const handleDoubleClick = (event) => {
console.log("Event Object:", event);
console.log("Mouse X Position:", event.clientX);
setNum(num + 1);
};
return (
<div>
<p>
Double-click the button to see the event details and increment the value
</p>
<button onDoubleClick={handleDoubleClick}>Double-Click Me</button>
<p>Current Value: {num}</p>
</div>
);
}
export default AccessEventObjectComponent;
Output
Accessing the Event ObjectIn this example
- The handleDoubleClick function logs the event object and the mouse's X position (event.clientX) when the button is double-clicked.
- It also increments the value of num.
Preventing Default Behavior
In React, event.preventDefault() is used to prevent the default behavior associated with certain events. It is commonly used to stop actions such as form submission, link navigation, or text selection from occurring, giving developers full control over how events are handled.
JavaScript
import React from "react";
function PreventDefaultComponent() {
const handleDoubleClick = (event) => {
event.preventDefault();
console.log("Text selection prevented!");
};
return (
<div>
<p onDoubleClick={handleDoubleClick}>Click Me</p>
</div>
);
}
export default PreventDefaultComponent;
Output
Preventing Default BehaviorIn this example
- handleDoubleClick function: Triggered when the paragraph is double-clicked. It calls event.preventDefault() to prevent the default behavior (text selection).
- console.log: Logs a message to the console ("Text selection prevented!") to confirm the prevention.
- Effect: Normally, double-clicking selects the text, but in this example, the text won't be selected due to preventDefault().
Using onDoubleClick for Custom Feature
The onDoubleClick event in React can be used to trigger custom functionality when a user double-clicks an element. This allows you to implement features such as toggling UI states, updating content, or performing specific actions only after a double-click.
JavaScript
import React, { useState } from "react";
function ToggleModeComponent() {
const [isEditing, setIsEditing] = useState(false);
const handleDoubleClick = () => {
setIsEditing(!isEditing);
};
const paragraphStyle = {
cursor: "pointer",
padding: "10px",
border: "1px solid #ccc",
borderRadius: "5px",
textAlign: "center",
fontSize: "18px",
backgroundColor: isEditing ? "#e0f7fa" : "#f1f1f1",
};
const modeTextStyle = {
fontWeight: "bold",
color: isEditing ? "#00796b" : "#212121",
};
return (
<div style={{ margin: "20px", textAlign: "center" }}>
<p onDoubleClick={handleDoubleClick} style={paragraphStyle}>
<span style={modeTextStyle}>
{isEditing ? "Edit Mode" : "View Mode"}
</span>
</p>
</div>
);
}
export default ToggleModeComponent;
Output
onDoubleClick for CustomIn this example
- This React component toggles between "Edit Mode" and "View Mode" on double-click.
- It uses the isEditing state to switch modes and changes the background and text color based on the mode.
- The paragraph is styled to be clickable with a pointer cursor.
Key features of the onDoubleClick event
- Triggered on Double-Click: Activates when a user double-clicks an element.
- Event Handler: Accepts a function that runs when the event occurs.
- Prevent Default Behavior: Can prevent default browser actions using event.preventDefault().
- Custom Action: Useful for toggling states, UI changes, or triggering specific actions based on double-clicks.
- Cross-Browser Compatibility: React normalizes events across browsers using the synthetic event system.
Conclusion
The onDoubleClick event in React is a powerful and useful tool for handling double-click interactions. It provides a way to trigger actions that require a double-click, such as editing content, toggling states, and handling complex user interactions like image zooming.
Similar Reads
JavaScript Tutorial JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. It's an interpreted language that executes code line by line, providing more flexibility.JavaScript on Client Side: On the client side, Jav
11 min read
Web Development Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
React Interview Questions and Answers React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook, created React. Developers with a JavaScript background can easily develop web applications
15+ min read
React Tutorial React is a powerful JavaScript library for building fast, scalable front-end applications. Created by Facebook, it's known for its component-based structure, single-page applications (SPAs), and virtual DOM,enabling efficient UI updates and a seamless user experience.Note: The latest stable version
7 min read
JavaScript Interview Questions and Answers JavaScript is the most used programming language for developing websites, web servers, mobile applications, and many other platforms. In Both Front-end and Back-end Interviews, JavaScript was asked, and its difficulty depends upon the on your profile and company. Here, we compiled 70+ JS Interview q
15+ min read
Domain Name System (DNS) DNS is a hierarchical and distributed naming system that translates domain names into IP addresses. When you type a domain name like www.geeksforgeeks.org into your browser, DNS ensures that the request reaches the correct server by resolving the domain to its corresponding IP address.Without DNS, w
8 min read
HTML Interview Questions and Answers HTML (HyperText Markup Language) is the foundational language for creating web pages and web applications. Whether you're a fresher or an experienced professional, preparing for an HTML interview requires a solid understanding of both basic and advanced concepts. Below is a curated list of 50+ HTML
14 min read
NodeJS Interview Questions and Answers NodeJS is one of the most popular runtime environments, known for its efficiency, scalability, and ability to handle asynchronous operations. It is built on Chromeâs V8 JavaScript engine for executing JavaScript code outside of a browser. It is extensively used by top companies such as LinkedIn, Net
15+ min read
Web Development Technologies Web development refers to building, creating, and maintaining websites. It includes aspects such as web design, web publishing, web programming, and database management. It is the creation of an application that works over the internet, i.e., websites.To better understand the foundation of web devel
7 min read
CSS Tutorial CSS stands for Cascading Style Sheets. It is a stylesheet language used to style and enhance website presentation. CSS is one of the three main components of a webpage, along with HTML and JavaScript.HTML adds Structure to a web page.JavaScript adds logic to it and CSS makes it visually appealing or
7 min read