I am trying to create multiple refs based on the length of input array.
export default function test(props) {
const numRefs = props.data.length;
let [refs, setRefs] = useState([]);
for (let i = 0; i < numrefs; i++) {
let ref = useRef(null);
setRefs(result => [...result, ref]);
}
return (
<div>
// Then I want to return numRefs number of divs each with a ref = one of ref from refs array
<div ref = {}></div>
</div>
)
}
React doesn't allow you to have hooks inside a loop and I am getting the error ->
' React Hook "useRef" may be executed more than once. Possibly because it is called in a loop. React Hooks must be called in the exact same order in every component render'
Is it possible to achieve this?