copy to clipboard in react.js

1 month ago23k times

copy to clipboard items using react useState Hook with the following code


  var maincode = "Text to be copied";
  const [codeText,SetCodeText] = useState("copy");
  const CopyCode = () =>{
    navigator.clipboard.writeText(maincode);
    SetCodeText("copied");
    setTimeout(()=>{
        SetCodeText("copy");    
    },2000);
  }

Add Answer

Add a codeAdd Code
Remove adsremove