How to copy to clipboard in React?

Chay Auker
2 min readJul 30, 2021

For my time using copy to clipboard on other websites, I have developed the desire to implement this function into applications. I recently learned how to accomplish this and would like to show all of you.

  1. Install npm react-copy-to-clipboard by using the below code
npm install --save react-copy-to-clipboard

2. Next is to import {CopyRoClipboard} within the blue box to React app

3. Add the “input” form and “CopyToClipboard” to the function and create default state “entryValue” to empty string(“”) as follows:

4. Then set type to text, value to entryValue, and onChange function to the value that the user inputs as follows:

5. Finally, set text to state (entryValue). Whatever you set “text” equal to will be copied to the clipboard after the user types and clicks the copy button. This allows for easy pasting in the future as desired.

--

--