i'm trying to learn about api fetching, and i followed some other answers on here, but i keep getting a weird error on this script:
const fetcher = async () => {
const url = 'https://banana1.free.beeceptor.com/cars'
const response = await fetch(url) //once we fetch we get a resposne
const json_data = await response.json()
return json_data
}
const Publish = () => {
const [loading, setLoading] = useState(true)
const [choices, setChoices] = useState([])
useEffect(()=>{
const choiceGetter = async () => {
await fetcher().then(json_data=>{
setChoices(json_data);
setLoading(false);
console.log(json_data)
})
};
choiceGetter();
},[])
return(
<>
<h1>
{loading&&<>loading...</>}
{!loading&&choices&&
choices.map( (choice) => {
const {option,make} = choice;
return( <div key={car1}>
<h1>{option}</h1>
<h3>{make}</h3>
</div>)
} )
}
</h1>
</>
)
}
export default ApiTest
the error:
Unhandled Runtime Error
TypeError: choices.map is not a function
Source
.next/static/chunks/pages/index.js (56:24) @ ApiTest
54 | {loading&&<>loading...</>}
55 | {!loading&&choices&&
> 56 | choices.map( (choice) => {
| ^
57 | const {option,make} = choice;
58 | return( <div key={car1}>
59 | <h1>{option}</h1>
i've tried to even mimic the following:
ReactJs useState .map() is not a function
but it's not working...