react clipboard code example
Example 1: react copy to clipboard
onClick={() => {navigator.clipboard.writeText(this.state.textToCopy)}}
Example 2: how to copy to clipboard in react js
<button
onClick={() => navigator.clipboard.writeText('Copy this text to clipboard')}
>
Copy
</button>
Example 3: react copy to clipboard button
<buttononClick={() => navigator.clipboard.writeText("Copy this text to clipboard")}>
Copy
</button>
Example 4: copy to clipboard reatjs
import React, { useRef, useState } from 'react';
export default function CopyExample() {
const [copySuccess, setCopySuccess] = useState('');
const textAreaRef = useRef(null);
function copyToClipboard(e) {
textAreaRef.current.select();
document.execCommand('copy');
e.target.focus();
setCopySuccess('Copied!');
};
return (
<div>
{
document.queryCommandSupported('copy') &&
<div>
<button onClick={copyToClipboard}>Copy</button>
{copySuccess}
</div>
}
<form>
<textarea
ref={textAreaRef}
value='Some text to copy'
/>
</form>
</div>
);
}