Open In App

What is onCopyCapture Event in ReactJS ?

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

React onCopyCapture is an event handler that gets triggered whenever we copy the text on the webpage. like onCopy, but the difference is that onCopyCapture acts in the capture phase whereas onBlur acts in the bubbling phase i.e. phases of an event.

Prerequisites:

Syntax:

<element onCopyCapture={function}/>

Steps to Create the React Application:

Step 1: Create a react project folder, open the terminal, and write the command.

npm create-react-app project

Step 2: After creating your project folder(i.e. project), move to it 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 putting a text within a <p> tag that is “Copy this Text”. After the element gets rendered whenever we copy the text, the onCopyCapture event calls the onCopyCaptureHandler a function that will show a text “copied”.

Javascript




function App() {
    const onCopyCaptureHandler = () => {
        console.log("copied");
    };
    return (
        <div className="App">
            <h1> Hey Geek!</h1>
            <p onCopyCapture={onCopyCaptureHandler}>
                Copy this text!
            </p>
 
        </div>
    );
}
 
export default App;


Step to Run 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