copy text to clipboard reactjs
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 leave page warning
- How to make remove buttoon on table using js DOM
- generate random password
- declare function javascript
- for each python json
- how to get time and date from iso string javascript
- dynamodb pagination nodejs
- angular back button
- how to code a minecraft json file mod
- html select specify deafult select by js variable
- install nuxt js
- javascript async await for x seconds
- get date and change format and increase date or month in javascript jquery
- navigation in react native
- angular limit string length
- how to run different node app on server different domains
- ng2 validations angular using reactiveforms
- dont starve together
- using objects for lookups in javascript
- how to select a class and then change the children of that class with javascript
- what is ... in javascript
- form data-request octobercms
- javascript add nd st th to number
- access to model from js