I'm trying to loop through an array and fetch those results to display individually on my site but I keep getting the following error message:
TypeError: this.props.fetched.map is not a function
Not sure why I'm getting the error. Any ideas?
This is the code I'm currently working on:
import React from "react";
import "isomorphic-unfetch";
export default class HomePage extends React.Component {
static async getInitialProps() {
const todoIdList = [1, 2, 3, 4];
for (const id of todoIdList) {
const response = await fetch(
`https://jsonplaceholder.typicode.com/todos/${id}`
);
const todo = await response.json();
return { fetched: todo.title };
}
}
render() {
return (
<>
<div>
{this.props.fetched.map((datafetch, index) => (
<p>{datafetch}</p>
))}
</div>
</>
);
}
}