Last active
July 27, 2020 21:43
-
-
Save onedebos/8b96632998324ceccf766edbb8338a40 to your computer and use it in GitHub Desktop.
This file contains 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, {useRef} from 'react' | |
const UseRefEx = () =>{ | |
let usernameRefs = useRef([]); | |
usernameRefs.current = [0,0,0,0].map( | |
(ref, index) => usernameRefs.current[index] = React.createref() | |
) | |
const handleCopyUsername = (e, index) => { | |
usernameRefs.current[index].current.select(); | |
document.execCommand("copy"); | |
}; | |
return( | |
<div> | |
[0,0,0,0].map((el, index)=> { | |
return ( | |
<div> | |
<input type="text" readonly ref={usernameRefs.current[index]} value={index} /> | |
<button onClick={handleCopyUsername}>Click to copy text </button> | |
</div> | |
)} | |
) | |
</div> | |
) | |
} | |
export default UseRefEx; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment