i am using fetch API to fetch from an API.
After fetching the result i want to setState to the reult which is not working
this is the function that fetches the results
export const random = (min, max) => Math.floor(Math.random() * (max - min)) + min;
export const getPokemons = async function () {
const pokemon = [];
for (let i = 0; i < 5; i++) {
pokemon.push(fetch(`https://pokeapi.co/api/v2/pokemon/${random(1, 800)}`));
}
return Promise.all(pokemon)
.then((data) => {
const parsedData = [];
data.forEach(async (d) => {
const resJson = await d.json()
parsedData.push(resJson);
});
return parsedData
})
.catch((err) => {
console.log(err);
});
};
the code where it is imported
const [stats,setStats] = useState([])
useEffect(()=>{
generateTrio()
},[])
async function generateTrio() {
const gTrio = await getPokemons();
setStats(gTrio)
console.log(stats,gTrio)
}
stats here is an empty array , while gTrio is required results i want to be in setStats