I am trying to write a function so I can copy the current browser's url to the clipboard when clicking on a link. I am still trying to understand this code, at this point I don't know how to create an input, set its value to the URL of the current document, select its contents and execute copy, as I understand it is the way to hack it. Right now when clicking on the text I get an error: Cannot read property 'select' of null... Any cue would be appreciated.
export function Copy() {
const [copySuccess, setCopySuccess] = useState("")
const textAreaRef = useRef(null)
function copyToClip(e) {
//navigator.clipboard.writeText(e.target.getAttribute("href"))
textAreaRef.current.select()
document.execCommand("copy")
setCopySuccess("Copied")
}
return (
<>
{document.queryCommandSupported("copy") && (
<Text onClick={copyToClip}>
Copy
</Text>
)}
</>
)
}