Problem : I am trying to create an array of 4 things from a list but the while loop always produces an infinite loop.
const [options, setOptions] = useState([]);
const getThings = () => {
while(options.length < 4) {
let randomThing =
listOfThings[Math.floor(Math.random()*listOfThings.length)];
!options.includes(randomThing) && setOptions([...options, randomThing]);
}
};
I believe the problem is connected to another issue - when I call the function once, it is randomly called anywhere between 2 - 9 times even without the while loop attached. Still trying to figure out why it keeps randomly firing so much.
getThings();