How To Copy Text To The Clipboard In ReactJS?
Last Updated :
14 Oct, 2024
In ReactJS, you can copy text to the clipboard using two methods. One way is to use the copy-to-clipboard package, which provides a simple copy() function to handle the task. Alternatively, you can use the browser’s built-in window.navigator.clipboard.writeText() method, which copies text to the clipboard directly by calling navigator.clipboard.writeText(‘your text’). Both methods make it easy to copy text efficiently in your React applications.
Prerequisites
Below are 2 approaches mentioned to copy text to the clipboard using React:
Steps To Copy Text to the Clipboard
Step 1: Create React Project using the following command.
npm create-react-app react-copy-text
cd react-copy-text
Step 2: Install there required dependencies.
npm i --save styled-components copy-to-clipboard
Project Structure

Folder structure
Dependencies
dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"copy-to-clipboard": "^3.3.3",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"styled-components": "^6.0.9",
"web-vitals": "^2.1.4"
}
Approach 1: Using copy-to-clipboard
To copy text to the clipboard in React we will use the NPM package copy-to-clipboard which enables the copying of any text from the input box or value stored in any component.
Example: Created to input box and link the first input box value to a useState hook instance. Pass this value in teh copy method that trrigers when the given button is clicked.
JavaScript
// Filename - App.js
import Clipboard from "./components/Clipboard.js";
function App() {
return <Clipboard />;
}
export default App;
JavaScript
//Filename - components/Clipboard.js
import React, { useState } from "react";
import copy from "copy-to-clipboard";
import {
Heading,
Input1,
Input2,
Container,
Button,
} from "./Styles";
const Clipboard = () => {
const [copyText, setCopyText] = useState("");
const handleCopyText = (e) => {
setCopyText(e.target.value);
};
const copyToClipboard = () => {
copy(copyText);
alert(`You have copied "${copyText}"`);
};
return (
<div>
<Heading>GeeksforGeeks </Heading>
<Container>
<h2> Copy To Clipboard in React JS</h2>
<Input1
type="text"
value={copyText}
onChange={handleCopyText}
placeholder="Enter the text you want to copy"
/>
<Button onClick={copyToClipboard}>
Copy to Clipboard
</Button>
<Input2
type="text"
placeholder="Enter the text you have copied"
/>
</Container>
</div>
);
};
export default Clipboard;
JavaScript
// Filename - Styles.js
import styled from "styled-components";
export const Container = styled.div`
padding: 2%;
max-width: 600px;
margin: 40px auto;
position: relative;
text-align: center;
`;
export const Heading = styled.h1`
text-align: center;
color: green;
font-size: 40px;
`;
export const Input1 = styled.input`
height: 50px;
width: 80%;
padding: 0;
font-size: 25px;
border-radius: 5px;
`;
export const Input2 = styled.input`
height: 50px;
width: 80%;
padding: 0;
font-size: 25px;
margin-top: 50px;
border-radius: 5px;
`;
export const Button = styled.button`
padding: 10px;
font-size: 20px;
position: relative;
// left: 30%;
margin-top: 10px;
cursor: pointer;
color: white;
background-color: green;
border-radius: 10px;
`;
To start the application run the following command.
npm start
Output: Now open your browser and go to http://localhost:3000/, you will see the following output:

Copy Text using Using copy-to-clipboard
Approach 2: Using window.navigator.clipbord method
We will be using the navigator property from window global object to make the text copy to clipboard.
Example: This approach includes the use of window.navigator.clipboard.writeText method to write the data. The value in the textbox will be copied to clipboard when the button is clicked.
CSS
/* Filename - App.css */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.App {
width: 100%;
max-width: 90%;
margin: auto;
text-align: center;
}
.geeks {
text-align: center;
color: green;
font-size: 40px;
}
textarea {
height: 50px;
width: 80%;
padding: 0;
font-size: 25px;
border-radius: 5px;
margin: auto;
margin-top: 2%;
}
button {
margin: 2%;
padding: 10px;
font-size: 20px;
position: relative;
/* // left: 30%; */
margin-top: 50px;
cursor: pointer;
color: white;
background-color: green;
border-radius: 10px;
}
JavaScript
// Filename - App.js
import Clipboard from "./components/Clipboard.js";
import "./App.css";
function App() {
return <Clipboard />;
}
export default App;
JavaScript
//Filename - components/Clipboard.js
import React, { useState } from "react";
const Clipboard = () => {
const [text, setText] = useState(
"Add text you want to copy"
);
const handleCopyClick = async () => {
try {
await window.navigator.clipboard.writeText(text);
alert("Copied to clipboard!");
} catch (err) {
console.error(
"Unable to copy to clipboard.",
err
);
alert("Copy to clipboard failed.");
}
};
return (
<div className="App">
<h1 className="geeks">GeeksforGeeks</h1>
<textarea
value={text}
onChange={(e) => setText(e.target.value)}
/>
<br />
<button onClick={handleCopyClick}>
Copy to Clipboard
</button>
<textarea />
</div>
);
};
export default Clipboard;
Output: Now open your browser and go to http://localhost:3000/, you will see the following output:

Copy Text To The Clipboard In ReactJS
Similar Reads
How to Copy Text to the Clipboard in React.js ?
In ReactJS, you can copy text to the clipboard using two methods. One way is to use the copy-to-clipboard package, which provides a simple copy() function to handle the task. Alternatively, you can use the browser's built-in window.navigator.clipboard.writeText() method, which copies text to the cli
4 min read
How to Copy Text to the Clipboard in NextJS?
Copy text to the clipboard empowers users to eÂffortlessly duplicate content and transfeÂr it across different applications or documents. This conveÂnient functionality simplifies information sharing and eleÂvates user experience by facilitating seamleÂss data transfer betweeÂn platforms. Prerequisi
3 min read
How to Copy Text to the Clipboard in React Native ?
Mobile app deÂvelopment requires enabling text copying to the clipboard for seÂamless content sharing and improved useÂr experienceÂ. A popular cross-platform framework, React Native, offers a solution to this common requirement. This article will guide developeÂrs through implementing clipboard fun
3 min read
How to Copy the Text to the Clipboard in JavaScript?
The document.execCommand("copy") method is commonly used to Copy the Text to the Clipboard, allowing developers to copy text programmatically, making it available for pasting elsewhere. To copy text from an input box using JavaScript, first use 'document.getElementById()' to access the input element
1 min read
How to Set Text Color in ReactJS ?
React provides you the ability to create interactive and dynamic useÂr interfaces. Within these interfaces, the choice of text color holds significant importance as it enhanceÂs the visual appeal and engageÂment for users. A foundational aspect of styling revolveÂs around modifying text color. In t
3 min read
How to update the State of a component in ReactJS ?
To display the latest or updated information, and content on the UI after any User interaction or data fetch from the API, we have to update the state of the component. This change in the state makes the UI re-render with the updated content. Prerequisites: NPM & Node.jsReact JSReact StateReact
3 min read
How to Create Copy to Clipboard Button?
A copy-to-clipboard button allows users to quickly copy text or other content to their device's clipboard, making it easy to paste the content elsewhere. This feature is commonly used in websites and applications to improve user experience by reducing the need for manual copying. In this article, we
3 min read
How to write ReactJS Code in Codepen.IO ?
Now everything is online, some people use VScode to write react.js code and face most of the difficulty. The VScode requires setting for writing React.js code and Many beginners faced difficulty to use VScode so, for them, it is good and easy to use codepen. The codepen provide you with an online pl
2 min read
How to Disable Text Selection in ReactJS ?
In this article, we will see how to disable the text selection in ReactJS. Prerequisites:Introduction to ReactReact HooksNPM or NPXWeb applications ofteÂn provide a valuable feature called text seleÂction, enabling users to easily highlight and copy content from a webpage. However, there are situat
4 min read
How To Add Strikethrough On Text In ReactJs ?
Strikethrough text is a visual representation often used to denote changes or deleted content within an application. In this article, we will see various techniques to effectively add a strikethrough to Text using ReactJs. Strikethrough text, also referred to as crossed-out or deleted text serves as
4 min read