I have the following problem:
There is child component that accepts a ref passed down from the parent component:
const ChildComp = React.forwardRef((props, ref) => <div ref={ref} />)
The parent component creates an array of refs and assigns a ref in the array to one of the children:
const ParentComp = () => {
const items = [1,2,3]
const refs = items.map(() => React.createRef())
React.useEffect(() => {
console.log(refs);
}, [refs])
return <div>{items.map((item, index) => <ChildComp ref={refs[index]>)}</div>
}
However, when I output the state of the refs array in useEffect, it outputs it only once when the parent mounts and at that point the ref.current values are still null, because children are not mounted yet.
I would like to be able to have an array of refs where each ref belongs to a child as illustrated above.