Skip to content

Instantly share code, notes, and snippets.

@supahfunk
Last active June 23, 2021 13:40
Show Gist options
  • Save supahfunk/3a7b90989a7107222143a1621bcc8dda to your computer and use it in GitHub Desktop.
Save supahfunk/3a7b90989a7107222143a1621bcc8dda to your computer and use it in GitHub Desktop.
React useRef in map
const MyComponent = ({ items }) => {
const $items = useRef(items.map(createRef))
useEffect(() => {
console.log($items.current[0].current)
}, [])
return (
{items.map((item, index) => (
<div
key={index.toString()}
ref={$items.current[index]}
>
{item.name}
</div>
))}
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment