copy to copied text in react code example

Example 1: react copy to clipboard

onClick={() => {navigator.clipboard.writeText(this.state.textToCopy)}}

Example 2: 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) {;
    // This is just personal preference.
    // I prefer to not show the the whole text area selected.;

  return (
       /* Logical shortcut for only displaying the 
          button if the copy command exists */
       document.queryCommandSupported('copy') &&
          <button onClick={copyToClipboard}>Copy</button> 
          value='Some text to copy'