Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react: copy component state value to clipboard without dummy element

Tags:

clipboard

In my project there is one usage case: user click one button and then copy some data to clipboard for next step.

The copied data is related to the clicked button, and is stored in the component state.

I do some search, and find the potential solution as following:

function copyToClipboard(text){
    var dummy = document.createElement("input");
    document.body.appendChild(dummy);
    dummy.setAttribute('value', text);
    dummy.select();
    document.execCommand("copy");
    document.body.removeChild(dummy);
}

to some extend, we need to create a dummy element, set the copied data to the dummy element and select the element, then execute the execCommand(copy) method.

is it possible to do this without creating dummy element? I know there are some react plugin about clipboard, but I just want to use vanilla javascript. thank you

like image 302
Chris Bao Avatar asked Oct 22 '18 06:10

Chris Bao


People also ask

How to copy text from one page to the clipboard using React?

Create react app First, we will have a simple react application. For that use the following command to set up the startup react application. 2. Design a page To design a page, we will use the textarea for entering the text and button for copy to clipboard. App.js 3. Implement logic for copy text to the clipboard

How do I copy text from one element to the clipboard?

The onCopy prop is run when the text is copied. Inside the component, we have the content that we can click to do the copying. Once the element is clicked, the content in the text prop will be copied to the clipboard. We can also use the execCommand method to copy the content of a DOM element that’s selected to the clipboard.

How to use execcommand to copy text to clipboard?

Would try to use execCommand with fallback to IE specific clipboardData interface and finally, fallback to simple prompt with proper text content & 'Copy to clipboard: Ctrl+C, Enter' Don't forget to manually install peer dependencies ( react) if you use npm@3. result (bool): Returns true if copied successfully, else false.

How to copy text from onclick to clipboard in PHP?

Now when we click on the button ‘Copy to Clipboard’, the function copyToClipboard gets triggered through onClick event which copies the state value to the clipboard with copy () function. Now we can copy our text anywhere by just clicking Ctrl+V key. Output: Now open your browser and go to http://localhost:3000/, you will see the following output:


1 Answers

Your solution works well.

If the value you want to copy is not yet rendered on the DOM, your Document.createElement('input')... method is a good way to create a document node that Document knows about, but that is not visible to the user. Once you use .createElement() you can then call execCommand() on it to copy the value to the clipboard.

The execCommand() method is exposed by HTML5's Document. This means Document has to know about the node you are targeting before you can use the method (this is called Selection).

However, if you want to copy text from an element already rendered on the dom (e.g an input in a form), you could use React's callback ref. Here's a good example of using ref to do this. It's pretty simple, so using a library is likely to be overkill.

like image 119
Jordan Rolph Avatar answered Oct 07 '22 09:10

Jordan Rolph