Skip to content

Instantly share code, notes, and snippets.

@memon07
Created December 28, 2019 10:44
Show Gist options
  • Save memon07/890d7d51a0f43e053de3bc288c2e8535 to your computer and use it in GitHub Desktop.
Save memon07/890d7d51a0f43e053de3bc288c2e8535 to your computer and use it in GitHub Desktop.
Clipboard Copy in react js (without any npm)
import React , { useState } from 'react'
function Clipboard {
const [copySuccess , setCopySuccess] = useState(false)
const [textArea, setTextArea] = useState()
function copyCodeToClipboard () {
const el = textArea
el.select()
document.execCommand("copy")
setCopySuccess(true)
}
return(
<>
<textarea
ref={(textarea) => setTextArea(textarea)}
value="This message will be copied."
/>
<button onClick={() => copyCodeToClipboard()}>
Copy to Clipboard
</button>
</>
)
}
export default Clipboard
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment