Copy the text to the clipboard in React

Use the navigator.clipboard.writeText() function.

For example, let's assume that you create an input with a button to copy the text on click:

import React, { useState } from 'react';

const CopyToClipboard = () => {
    const [text, setText] = useState('');
    const copyText = () => {
        <div className = 'CopyToClipboard'>
            <input  onChange = {(e) => setText(} value = {text}/>
            <button onClick = {copyText}>Click to copy</button>

export default CopyToClipboard;

Hi, I'm Erik, an engineer from Barcelona. If you like the post or have any comments, say hi.