2024-10-01 Web Development
Copy to Clipboard for Text in JavaScript
By O. Wolfson
Copying content to the clipboard is a powerful way to improve the user experience in your app. Modern JavaScript and browser APIs make this easier to implement. In this article, we’ll cover how to copy text to the clipboard with practical a example using a React component, suitable for a Next.js app.
1. Copying Text to Clipboard
Copying text to the clipboard is the most straightforward use case, and the navigator.clipboard.writeText
API handles this elegantly. Here’s a simple React component that demonstrate copying text to the clipboard.
Click the button to copy the text below:
Hello, world!
This component allows users to click a button to copy a hardcoded string to the clipboard.