I'm trying to access a an array of refs during clean up (before the component unmounts). Like so:
const refs = useRef([]);
useEffect(() => {
let localRefs = null;
if (refs.current) {
localRefs = refs.current;
}
return () => {
console.log("unmounting", localRefs);
};
}, []);
return (
<>
<div
ref={(el) => {
refs.current[0] = el;
}}
></div>
<div
ref={(el) => {
refs.current[1] = el;
}}
></div>
<div
ref={(el) => {
refs.current[2] = el;
}}
></div>
</>
);
In this case, the log outputs null. The refs appear to be created and stored properly on mount, but appear as an array with 3 null entries when logged during the cleanup.