Answers for "copy text to clipboard reactjs"

14

react copy to clipboard

onClick={() => {navigator.clipboard.writeText(this.state.textToCopy)}}
Posted by: Guest on April-14-2020
0

react copy to clipboard

<button 
  onClick={() =>  navigator.clipboard.writeText('Copy this text to clipboard')}
>
  Copy
</button>
Posted by: Guest on August-10-2021
1

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>
  );
}
Posted by: Guest on May-05-2020

Code answers related to "copy text to clipboard reactjs"

Code answers related to "Javascript"

Browse Popular Code Answers by Language