Open In App

What is onMouseDownCapture Event in ReactJS?

Last Updated : 28 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

React onMouseDownCapture is an event handler that gets triggered whenever we press the mouse button. like onMouseDown, but the difference is that onMouseDownCapture acts in the capture phase whereas onMouseDown acts in the bubbling phase i.e. phases of an event.

Prerequisites:

Syntax:

<element onMouseDownCapture={function}/>

Steps to Create the React Application:

Step 1: Create a react project folder

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:

Project Structure

The updated dependencies in package.json file will look like:

"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4",
}

Example: In this example, we are adding a text within the <p> tag i.e. “Click Me!”, when the user will click on this element the onMouseDownCapture event will call onMouseDownCaptureHandler, a function that will show a text “onMouseDownCapture Event!” whenever we press the mouse button over the element.

Javascript




function App() {
    const onMouseDownCaptureHandler = () => {
        console.log("onMouseDownCapture Event!");
    };
    return (
        <div className="App">
            <h1> Hey Geek!</h1>
            <p onMouseDownCapture={onMouseDownCaptureHandler}>
                Click Me!
            </p>
 
        </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:

Output


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads