Skip to content

Instantly share code, notes, and snippets.

@onedebos

onedebos/useRefEx.jsx

Last active Jul 27, 2020
Embed
What would you like to do?
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