DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Migrating from React Router v5 to v6: A Comprehensive Guide
  • Build Your Own GitHub-Like Tool With React in One Hour
  • Ditch Your Local Setup: Develop Apps in the Cloud With Project IDX
  • How to Create a Pokémon Breeding Gaming Calculator Using HTML, CSS, and JavaScript

Trending

  • Integration Isn’t a Task — It’s an Architectural Discipline
  • A Simple, Convenience Package for the Azure Cosmos DB Go SDK
  • A Modern Stack for Building Scalable Systems
  • Unmasking Entity-Based Data Masking: Best Practices 2025
  1. DZone
  2. Coding
  3. JavaScript
  4. How to Copy Text to Clipboard Using React.js

How to Copy Text to Clipboard Using React.js

In this brief tutorial, follow along with a demonstration of how to copy text to the clipboard in React.js applications.

By 
Sanwar Ranwa user avatar
Sanwar Ranwa
DZone Core CORE ·
Oct. 29, 24 · Code Snippet
Likes (4)
Comment
Save
Tweet
Share
4.4K Views

Join the DZone community and get the full member experience.

Join For Free

In this article, we will learn how to copy text to Clipboard using React.js. Using this, we can copy any text to the clipboard and paste it anywhere.

You can check my previous articles, in which we discussed React.js and its basic components from the below-mentioned links.

  • Upload Files Using ASP.NET Web API and React.js
  • Create User Registration and Login Using Web API and ReactJS
  • Internationalization In ReactJS Application Using i18Next

Prerequisites

  • Basic knowledge of React.js
  • Visual Studio Code IDE
  • Basic knowledge of Bootstrap and HTML

Create React.js Project

Let's first create a React application using the following command.

Java
 
npx create-react-app platform


Now, open the newly created project in Visual Studio Code and install Bootstrap by using the following command:

Java
 
npm install --save bootstrap  


Open the index.js file and import Bootstrap.

Java
 
import 'bootstrap/dist/css/bootstrap.min.css';


Now, install the copy-to-clipboard library using the following command. 

Java
 
npm install save copy-to-clipboard  


Go to the src folder, create a new component named CopyBoard.js, and add the following lines to this component.  

Java
 
import React, { Component } from 'react'  
import copy from "copy-to-clipboard";  
import './CopyBoard.css';  
export class CopyBoard extends Component {  
        constructor() {  
                super();  
                this.state = {  
                        textToCopy: "Copy to Clipboard Demo!",  
  
                };  
                this.handleInputChange = this.handleInputChange.bind(this);  
                this.Copytext = this.Copytext.bind(this);  
        }  
  
        handleInputChange(e) {  
                this.setState({  
                        textToCopy: e.target.value,  
                });  
        }  
  
        Copytext() {  
                copy(this.state.textToCopy);  
                
        }  
  
        render() {  
                const { textToCopy, btnText } = this.state;  
                return (  
                        <div className="container">  
                                <div class="row" className="hdr">  
                                        <div class="col-sm-12 btn btn-info">  
                                                Copy to Clipboard Demo  
            </div>  
                                </div>  
                                <div className="txt">  
                                        <textarea className="form-control" placeholder="Enter Text" onChange={this.handleInputChange} ></textarea>  
                                        <br />  
                                        <br />  
                                        <button className="btn btn-info" onClick={this.Copytext}>  
                                                Copy to Clipboard  
                    </button>  
                                </div>  
                           
                        </div>  
                );  
        }  
}  
  
export default CopyBoard 


Create a new CSS file and add the following CSS in this file. 

Java
 
.txt  
{  
        margin-bottom: 20px;  
      margin-top: 20px;  
}  
.hdr  
{  
        margin-top: 20px;  
}  


Open the App.js file and add the following code:

Java
 
import React from 'react';  
import logo from './logo.svg';  
import './App.css';  
import CopyExample from './CopyBoard';  
function App() {  
  return (  
    <div className="App">  
      <CopyExample></CopyExample>  
    </div>  
  );  
}  
  
export default App; 


Now run the project, and check the result. 

Copy to clipboard demo screenshot

Enter some text in the textbox and click on the button. 

Entering text into textbox

The text is now copied. Paste the text into Notepad.

Paste text to Notepad

Summary 

In this article, we learned how we can copy text to the clipboard in React.js applications.

API CSS Integrated development environment Bootstrap (front-end framework) React (JavaScript library)

Published at DZone with permission of Sanwar Ranwa. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Migrating from React Router v5 to v6: A Comprehensive Guide
  • Build Your Own GitHub-Like Tool With React in One Hour
  • Ditch Your Local Setup: Develop Apps in the Cloud With Project IDX
  • How to Create a Pokémon Breeding Gaming Calculator Using HTML, CSS, and JavaScript

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!