I have fetched json data from API and am trying to display it. Am able to console log the array but have been unable to display in a div. Map() function does not work on this data.
React / Next.js app
Trying to display json object in div but getting TypeError on map() function.
const getCurrentData = async () => {
const res = await fetch("http://api.weatherapi.com/v1/current.json?key=84703323c7f94238a98203306233010&q=London&aqi=no")
return res.json();
};
export default async function Weather() {
const current = await getCurrentData();
return (
<>
{current.map((current: any, index) => {
<div key="{index}">
<span>{current.location}</span>
<span>{current.humidity}</span>
</div>
})}
</>
);
}
2
Answers
I have refactored but still have an error:
Error: Objects are not valid as a React child....
React components can not be asynchronous functions. Instead they should be pure and isolate side effects.
Use useState to fetch data and use useState to then display it inside your application.