I'm trying to handle copy to clipboard in React with useRef hook. How can I achieve it without any additional libraries? Here is my code but it throws an error myRef.current.select is not a function.
import React, { useRef } from "react";
const Element = () => {
const myRef = useRef();
const copyToClipboard = () => {
myRef.current.select();
document.execCommand("copy");
};
return (
<div>
<span onClick={copyToClipboard} ref={myRef}>
Text to be copied
</span>
</div>
);
};
export default Element;