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
React onClick Event
The onClick event in React is used for handling a function when an element, such as a button, div, or any clickable element, is clicked. Syntax onClick={handleClick}Parameter The callback function handleClick which is invoked when onClick event is triggeredReturn Type It is an event object containin
3 min read
React onSubmit Event
The onSubmit event in React is an essential event handler used for managing form submissions. It helps you to implement custom logic when a user submits a form, such as a form validation, preventing default submission behaviour, and executing actions like processing data or making API calls. What is
5 min read
React onScroll Event
React onScroll helps to listen for scroll interactions within specific elements and trigger actions accordingly. It triggers whenever the scrolling position changes within the specified element. It is useful when implementing infinite scrolling, parallax effects, lazy loading, or dynamic UI changes.
4 min read
React onBlur Event
In React, handling user interactions efficiently is important for building responsive and dynamic applications. One of the commonly used events for managing focus behaviour is the onBlur event. This event is important for tracking when a user moves the focus away from an element, such as an input fi
7 min read
React onMouseUp Event
React onMouseUp event is used to detect when a mouse button is released over an element. It triggers when the user releases a mouse button while the cursor is over an element. It is particularly useful where you want to detect the ending of a mouse click or drag operation. It is similar to the HTML
2 min read
React onFocus event
The onFocus event in React is triggered when an element receives focus, meaning it becomes the active element that can accept user input. This event is commonly used to execute functions or perform actions when an element gains focus. It is similar to the HTML DOM onfocus event but uses the camelCas
2 min read
React onTouchEnd Event
React onTouchEnd event event fires when the user touches screen and releases the touch. Similar to other elements in it, we have to pass a function for process execution. It is similar to the HTML DOM ontouchend event but uses the camelCase convention in React. Syntax:onTouchEnd={function}Parameter
2 min read
React onTouchCancel Event
React onTouchCancel event fires when touch interrupts. Similar to other elements in it, we have to pass a function for process execution. It is similar to the HTML DOM touchcancel event but uses the camelCase convention in React. Syntax: onTouchCancel={function}Parameter : function that will call wh
2 min read
What is onDoubleClickCapture Event in ReactJS ?
React onDoubleClickCapture is an event handler that gets triggered whenever we double click the element. like ondblclick, but the difference is that onDoubleClickCapture acts in the capture phase whereas onDoubleClick acts in the bubbling phase i.e. phases of an event. Prerequisite: Introduction and
2 min read
React onMouseDown Event
The onMouseDown event is a native DOM event in React that triggers when the mouse button is pressed down on an element. It is part of a set of mouse events that React and the DOM handle, which includes events like onClick, onMouseUp, onMouseEnter, and others. onMouseDown occurs when any mouse button
5 min read