Skip to content

Instantly share code, notes, and snippets.

@onedebos
Last active July 27, 2020 21:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save onedebos/8b96632998324ceccf766edbb8338a40 to your computer and use it in GitHub Desktop.
Save onedebos/8b96632998324ceccf766edbb8338a40 to your computer and use it in GitHub Desktop.
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