I'm a complete beginner in react and I am fetching data from https://www.boredapi.com/documentation#endpoints-price . With http://www.boredapi.com/api/activity/ you get a random activity object in the format of
{
"activity": "Learn Express.js",
"accessibility": 0.25,
"type": "education",
"participants": 1,
"price": 0.1,
"link": "https://expressjs.com/",
"key": "3943506"
}
I'm trying to fetch 10 of these objects using react hooks and then display them . However with my code below when I try to fetch and display a single activity name nothing happens . I initialize an array with hooks and I want to append a new object each time I fetch
useFetch.js to fetch an activity
import {useState , useEffect} from 'react';
export default function useFetch(){
const [activities,setActivities] = useState([]);
const getActivities = async () =>{
for(let k=0;k<10;k++){
let response = await fetch("http://www.boredapi.com/api/activity/");
let result = await response.json();
setActivities((oldstate)=>[...oldstate,result])
}
}
useEffect(()=>{
getActivities();
}, [])
return {activities};
}
AcitivityBar.js to display activities
import React from 'react';
import useFetch from './useFetch';
export default function ActivityBar(){
const {activities} = useFetch();
console.log(activities)
return (
<div className="activities-container">
{
(activities.map((a , index)=>{
return <h1 key = {index}>hi</h1>
}))
}
</div>
);
}