Seize the moment to copy text to clipboard in React with minimal effort using navigator.clipboard.writeText():
const copyTextToClipboard = async (text) => {
try {
await navigator.clipboard.writeText(text);
console.log('Text swiftly copied to clipboard! ๐จ');
} catch (err) {
console.error('Houston, we have a problem ๐:', err);
}
};
Summon this superpower via:
<button onClick={() => copyTextToClipboard('Your precious text here')}>Copy</button>
Fast and efficient: just click the button and voila, you've copied!
Retrieves text from older scrolls (Handling older browsers)
Older browsers are like ancient scrolls, they need a translator. Use document.execCommand or window.clipboardData.setData as a scroll interpreter. ๐โ๏ธ๐ฌ