copy to clipboard in react.js

2 years 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

Latest codes

view all

Latest Snippets

view all