You will learn how to handle the onPaste event in React with examples. Some ways will be covered, for example, using the onPaste event attribute or assigning onPaste with a function method.
Handle the onPaste event in React with examples
The onpaste attribute is fired when the user pastes something into the tag. Although the onpaste attribute is supported in all HTML tags, it is not always possible to paste the content into a tag, for example, a <p> tag, unless that tag has the contenteditable attribute set to “true.”
Syntax:
<element onpaste="script">
There are 3 ways to paste the content into a tag:
- Press CTRL + DRAW
- Select “Paste” from the “Edit” menu on the browser
- Right-click and select the “Paste” command
Using the onPaste event attribute
This way, we will use the onPaste usage in the input tag or text fields like the commonly used events like onClick or Change. Editable elements will have this event built into them. You need to add the attribute, which will be received when the event is fired.
Code:
import React, { useState } from "react"; const App = () => { const [value, setValue] = useState(""); const handlePaste = () => { console.log(value); }; return ( <> <h2>Handle the onPaste event in React | LearnShareIT</h2> {value} <hr /> <form className="form"> <input value={value} onPaste={handlePaste} onChange={(e) => setValue(e.target.value)} /> </form> </> ); }; export default App;
Output:
When you paste a text into the input, the program can catch this event and run it through the handlePaste function so you can handle it easily. When users use the paste method, as I listed above, they are caught by the program.
Assigning onPaste with a function method
This way, we will assign a function to handle the onPaste event on the page. You can use this in a class component because React is a Javascript-based library. There’s little difference in event handling between ReactJS and Javascript. With Javascript, a function will be called to execute when an event occurs. But with React, a Component method is called when the event occurs.
Code:
onPaste: function() { console.log(this.value); },
As per the above replacement code, we have assigned an anonymous function to the onPaste event to handle this user event. The results returned to us are also the same. I wish you success with these methods.
Summary
To sum up, you have quickly learned two ways to handle the onPaste event in React with examples. However, it would help if you used the onPaste event attribute because it will be neater and easier to follow for the developer.
Maybe you are interested:
- How To Handle The OnKeyPress Event In React
- Handle the onKeyPress event in React
- Handle the Browser Tab close event in React

Hi, my name’s Nathaniel Kirk. I’m interested in learning and sharing knowledge about programming languages. My strengths are C, C++, Python, HTML, CSS, Javascript, and Reactjs. If you have difficulty with them, don’t worry. I’m here to support you.
Name of the university: PTIT
Major: IT
Programming Languages: C, C++, Python, HTML, CSS, Javascript, Reactjs