Array of React Refs
Need to make an array of refs with the useRef
hook. So how do I do add them programmatically inside map
or foreach
import React, {useRef} from 'react'
export const App = ( {doc} ) => {
const childrenRef = useRef([]);
childrenRef.current = []
const handleRef = (el) => {
if(el && !childrenRef.current.includes(el)){
childrenRef.current.push(el)
}
console.log('childrenRefs.current', childrenRef.current);
}
return(
{doc.sections.map((item, i) => {
if(!item.header) return null
return <p key={i} ref={handleRef}>{item.header}</p>
})}
)
}