Creating a Copy-Paste Functionality in React: A Step-by-Step Guide.

Software Engineer with 4 years of solid hands-on experience in Python Development, Scripting and Automation.
Adding copy and pasting the text feature to your website is very handy, it simplifies the process for the users, but are you finding difficulty in implementing it?
In this article, I’ve given step-by-step instructions on how to implement copy-paste text functionality.
I’ve mentioned the Github repo link at the end of the article.
Project Demo:

Import the required dependencies.
import { useRef } from 'react' import React from 'react' import ClipboardJS from 'clipboard'The useRef hook is used to access the text area element, ClipboardJS is a library for working with clipboard.
Define the functional component
CopyPastethat returns a form with a text area and two buttons.function CopyPaste() { const textAreaRef = useRef(null) ... return ( <div style={{ color: 'white' }}> <form> <label htmlFor="name">Name:</label> <input type="text" ref={textAreaRef} id="name" className="form-control mb-2" /> <button className="btn btn-outline-warning m-2 btn-copy" data-clipboard-target="#name" > Copy </button> <button className="btn btn-outline-info m-2" onClick={pasteFromClipboard} > Paste </button> </form> </div> ) }The text area is created with the
inputelement and therefattribute is set to thetextAreaRefto access it in the component.Implement the copy functionality.
React.useEffect(() => { new ClipboardJS('.btn-copy', { text: function(trigger) { return trigger.getAttribute('aria-label') } }) }, [])Here, the
useEffecthook is used to call theClipboardJSconstructor when the component is mounted. The.btn-copyselector is used to select the copy button, and the text in the text area with theidofnamewill be copied when the button is clicked.Implement the paste functionality.
const pasteFromClipboard = (e) => { e.preventDefault() navigator.clipboard .readText() .then((text) => { textAreaRef.current.value = text }) .catch((error) => { console.error(error) }) }The
pasteFromClipboardfunction reads the text from the clipboard usingnavigator.clipboard.readTextand updates the value of the text area with the returned text. Thee.preventDefaultis used to prevent the default behavior of the form submit.Export the component.
export default CopyPasteThe final code should look like this:
import { useRef } from 'react' import React from 'react' import ClipboardJS from 'clipboard' function CopyPaste() { const textAreaRef = useRef(null) React.useEffect(() => { new ClipboardJS('.btn-copy', { text: function(trigger) { return trigger.getAttribute('aria-label') } }) }, []) const pasteFromClipboard = (e) => { e.preventDefault() navigator.clipboard .readText() .then((text) => { textAreaRef.current.value = text }) .catch((error) => { console.error(error) }) } const copyToClipboard = (e) => { e.preventDefault() } return ( <div style={{ color: 'white' }}> <form> <label htmlFor="name">Name:</label> <input type="text" ref={textAreaRef} id="name" className="form-control mb-2" /> <button className="btn btn-outline-warning m-2 btn-copy" data-clipboard-target="#name" onClick={copyToClipboard} > Copy </button> <button className="btn btn-outline-info m-2" onClick={pasteFromClipboard} > Paste </button> </form> </div> ) } export default CopyPasteGitHub Repository Link: https://github.com/vishwanath16/React-Copy-Paste-Example
How to run the project?
Getting Started
To run the project, follow these steps:
- Clone the repository
git clone https://github.com/vishwanath16/React-Copy-Paste-Example.git
2. Install dependencies
cd react-copy-paste npm install
3. Start the development server
npm start
The project will be running at http://localhost:3000/.
Prerequisites
You will need the following tools to run this project:
Node.js
npm (Node Package Manager)
Built With
React — A JavaScript library for building user interfaces
ClipboardJS — A modern approach to copy text to clipboard




