Open In App

React onDoubleClick Event

Last Updated : 19 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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

onClickdbGIF
OutponDoubleClick Eventut

In 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

2click
Accessing the Event Object

In 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

Screenshot-
Preventing Default Behavior

In 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

view-1
onDoubleClick for Custom

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


Next Article

Similar Reads