Created
December 20, 2020 13:54
-
-
Save hungdoansy/8357805077b74be14c4449ecb26210f4 to your computer and use it in GitHub Desktop.
Use multiple refs for a list in React
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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