I'm new to React, sorry about this stupid question. I get the error:
Uncaught TypeError: products is not iterable
If I create a separate array (myArray) and pass this to setProducts, this works, but the loop doesn't when calling setProducts on each pass. Is useState() not allowed within a for loop?
const [products, setProducts] = useState([]);
let myArr = [];
function selectProducts() {
for (let i = 0; i < 4; i++) {
let rand = Math.floor(Math.random() * jsData.length + 1);
let item = jsData[rand];
//myArr.push(item);
setProducts([...products, item]);
}
//setProducts(myArr);
console.log(products);
}