📌  相关文章
📜  如何在 React js 中复制到剪贴板 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:02:21.140000             🧑  作者: Mango

代码示例4
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 (
    
{ /* Logical shortcut for only displaying the button if the copy command exists */ document.queryCommandSupported('copy') &&
{copySuccess}
}