Open In App

What is onDoubleClickCapture Event in ReactJS ?

Improve
Improve
Like Article
Like
Save
Share
Report

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:

Syntax:

<element onDoubleClickCapture={function}/>

Steps to create React Application:

Step 1: Create a React application using the following command.

npm create-react-app project

Step 2: After creating your project folder(i.e. project), move to it by using the following command.

cd project

Project Structure: It will look like this:

Example: In this example, we are creating a button “Double Click Me!”. Whenever we click on it twice the onDoubleClickCapture event will call onDoubleClickCaptureHandler, a function that will show a text “you have clicked twice” whenever we click the button twice.

App.js




function App() {
    const onDoubleClickCaptureHandler = () => {
        console.log("You have Clicked Twice");
    };
    return (
        <div className="App">
            <h1> Hey Geek!</h1>
            <button onDoubleClickCapture={
                    onDoubleClickCaptureHandler
                }>
                Double Click Me!
            </button>
        </div>
    );
}
 
export default App;


Step to Run the Application: Run the application using the following command from the root directory of the project.

npm start

Output:

Usage and benefits

1. Global Event Handling:

Unlike regular event handlers, onDoubleClickCapture allows you to capture double-clicks globally, irrespective of where the double-click occurs in the application. This can be useful where you need to track or handle double-clicks across different components or elements.

2. Preventing Bubbling:

By capturing the event at the top of the DOM, onDoubleClickCapture prevents the event from propagating further in the tree. This can be useful where you want to handle a double-click event without allowing other elements to receive the same event.


Last Updated : 28 Nov, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads