xxxxxxxxxx
import React from "react";
const CopyTextButton = () => {
const copyText = async () => {
try {
await navigator.clipboard.writeText("Text to be copied"); // Replace "Text to be copied" with the actual text you want to copy
console.log("Text copied successfully!");
} catch (err) {
console.error("Failed to copy text:", err);
}
};
return <button onClick={copyText}>Copy Text</button>;
};
export default CopyTextButton;
xxxxxxxxxx
// copy text on button click in react
const [textArea, setTextArea] = useState("THis is text")
const copyText = () => {
navigator.clipboard.writeText(textArea);
}