Created
December 28, 2019 10:44
-
-
Save memon07/890d7d51a0f43e053de3bc288c2e8535 to your computer and use it in GitHub Desktop.
Clipboard Copy in react js (without any npm)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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