Skip to content

Instantly share code, notes, and snippets.

@hungdoansy
Created December 20, 2020 13:54
Show Gist options
  • Save hungdoansy/8357805077b74be14c4449ecb26210f4 to your computer and use it in GitHub Desktop.
Save hungdoansy/8357805077b74be14c4449ecb26210f4 to your computer and use it in GitHub Desktop.
Use multiple refs for a list in React
/*
* This is helpful when you have multiple items and want to have some control over each of them.
*/
export const useMultipleRefs = (n) => {
const refs = useRef(
Array(n)
.fill(1)
.reduce((acc, _, index) => {
acc[index] = React.createRef();
return acc;
}, [])
);
return refs.current;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment