I have an array
let results = [
{id: 34},
{id: 32},
{id: 35},
]
When I use for each with useEffect, the request sends a lot. It repeats several times, but I only use one place. I wrote results in the dependencies, but it did not work. I need the requests send one time for ID
my function
useEffect(() => {
results.forEach(async (data) => {
const resp = await axios.get(`https://nehra.az/public/api/product/${data.id}`);
console.log(resp);
});
}, []);
My Network
useEffect is a side-effect to the rendering. The fact that this is triggered several times means that you had several renderings for the component. You will need to either use some property to know whether the requests were already sent, or move this code into a place that's executed exactly once.