EN
React - clipboard events
3 points
In this article, we would like to show you clipboard events in React.
There are three clipboard events:
- onCopy
- onCut
- onPaste
In the example below, we create three functions that handle onClick
, onCut
, and onPaste
events by displaying in the console which action has been performed.
Runnable example:
xxxxxxxxxx
1
// Note: Uncomment import lines while working with JSX Compiler.
2
// import React from 'react';
3
// import ReactDOM from 'react-dom';
4
5
const App = () => {
6
const handleCopy = () => {
7
console.log('you copied text!');
8
};
9
10
const handleCut = () => {
11
console.log('you cut text!');
12
};
13
14
const handlePaste = () => {
15
console.log('you pasted text!');
16
};
17
18
return (
19
<div>
20
<div>
21
<label>You can copy/cut text from here: </label>
22
<input
23
type="text"
24
onCopy={handleCopy}
25
onCut={handleCut}
26
onPaste={handlePaste}
27
defaultValue="Try to copy or cut this text"
28
/>
29
</div>
30
<div>
31
<label>You can paste text here: </label>
32
<input
33
type="text"
34
onCopy={handleCopy}
35
onCut={handleCut}
36
onPaste={handlePaste}
37
/>
38
</div>
39
</div>
40
);
41
};
42
43
const root = document.querySelector('#root');
44
ReactDOM.render(<App />, root );