Skip to content

Instantly share code, notes, and snippets.

@palanisamym14
Created August 11, 2020 12:20
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 palanisamym14/6e41de2b7604fc91cc99664d9fdaa1d9 to your computer and use it in GitHub Desktop.
Save palanisamym14/6e41de2b7604fc91cc99664d9fdaa1d9 to your computer and use it in GitHub Desktop.
const App = props => {
const itemsRef = useRef([]);
// you can access the elements with itemsRef.current[n]
useEffect(() => {
itemsRef.current = itemsRef.current.slice(0, props.items.length);
}, [props.items]);
return props.items.map((item, i) => (
<div
key={i}
ref={el => itemsRef.current[i] = el}
style={{ width: `${(i + 1) * 100}px` }}>
...
</div>
));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment