Overview:
I am trying to use React to display data from an API. I have the "building blocks" of what I need, but I am trying to figure out how to combine them.
Problem:
I have a page that gets Rendered, a fetch call that requests data from an API, and some code that loops through the returned object.
How do I combine these three things so that I can see the content rendered on the page?
Thanks!
Here is my React code:
import { ... } from "...";
const Index = () => (
<Page>
<Frame>
// HERE IS WHERE I WANT TO LOOP THROUGH THE API RESULTS
</Frame>
</Page>
);
export default Index;
Here is how I fetch data from an API:
function getDataFromApi() {
var url = "api.example.com";
return fetch(url, {
method: "GET",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
})
.then((response) => response.json())
.then((responseData) => {
return responseData;
})
.catch((error) => console.warn(error));
}
Here is how I'm looping through an object:
var i;
for (i = 0; i < objects.length; i++) {
var object = objects[i]
console.log(object);
}