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 structureDependencies
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-clipboardApproach 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 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 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 add styles to autocomplete in ReactJS ?
Autocomplete functionality in web applications is common for predictive user input. This article focuses on enhancing the visual appearance of ReactJS autocomplete components using Material-UI's package, offering customization options for seamless integration with the overall application design.Prer
3 min read
How to Make Text Blink in ReactJS ?
In this article, we are going to learn about making a text blink in React. Making text blink in React refers to the process of creating a visual effect where text repeatedly alternates between being visible and invisible, creating a flashing or blinking appearance, often achieved through CSS animati
3 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
How to use TextareaAutosize Component in ReactJS?
A textarea component for React that grows with content on Input. Material UI for React has this component available for us, and it is very easy to integrate. We can use the TextareaAutosize Component in ReactJS using the following approach.Prerequisites:NPM & Node.jsReact JSReact Material UIAppr
2 min read
How to get text inside Text component onPress in ReactJS ?
In this article, we will learn "How to get the text inside Text component onPress in ReactJS?". Problem Statement: Sometimes in an application, it is required to get the text inside certain components, in order to reuse the components again & again just by changing the text value. Approach: We w
4 min read
How To Use ComboBox in ReactJS?
When developing React applications, creating user-friendly forms with dropdowns or select inputs is common. However, sometimes you need more than just a static dropdown listâyou need a ComboBox. A ComboBox combines the functionality of a dropdown and an input field, allowing users to either select f
2 min read