I have a list of names inside an array that I need to get data for.
My goal is to use a for-loop that will iteratively perform an API call for each name in the list, with the data from every call being stored into an object inside a Person (useState) array. So far I've put together a component but it seems to cause the entire site to become glitchy and malfunction.
I believe it may be coming from the way I've used the loop inside the useEffect, but I'm not entirely sure. Is there a better approach to implementing the component below?
function API_CALL(){
const [Person, SetInfo] = useState([
{
age:'',
occupation:''
}
])
const names = [
"bob",
"alice",
"jerry",
"steve",
"tracy",
"hank"
]
useEffect(() => {
for(let i = 0; i <= 5; i++){
fetch('https://api.userfacesite.com/api/v3/data/' + names[i], {
headers: {
'Accept': 'application/json',
'Path': '/',
}
})
.then(response => response.json())
.then(json=>
SetInfo([...person, {age:json.person.age, occupation:json.person.occupation}])
);
}
},[Person]);
return(
<div>
{JSON.stringify(Person)}
</div>
)
}