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');
// This is just personal preference.
// I prefer to not show the the whole text area selected.
e.target.focus();
setCopySuccess('Copied!');
};
return (
<div>
{
/* Logical shortcut for only displaying the
button if the copy command exists */
document.queryCommandSupported('copy') &&
<div>
<button onClick={copyToClipboard}>Copy</button>
{copySuccess}
</div>
}
<form>
<textarea
ref={textAreaRef}
value='Some text to copy'
/>
</form>
</div>
);
}
Also in JavaScript:
- javascript on hover
- my datatable in flutter from json repeat the column headers
- Getting the differences between two objects javascript lib
- reverse a string javascript
- javascript how to reverse a string
- how to display first value of an array in jquery
- set dynamic route in link react js
- can filter be used on objects in javascript
- How to show confirm message before delete using jquery
- js replace space with underscore
- get value of ajax success in variable
- JavaScript replace all periods
- what called window.onerror
- javascript alphabet to number
- javascript random 4 digit number
- how to change package name in react native
- jquery advace
- angular redirect to external url
- javascript object to query string
- Multiple Identical Options in Switch Statements
- socket io query
- load_jsonl
- discord.js bot
- react dropdown menu stack overflow