【发布时间】:2020-09-26 01:41:39
【问题描述】:
我正在尝试遍历一个数组并获取这些结果以单独显示在我的网站上,但我不断收到以下错误消息:
TypeError:this.props.fetched.map 不是函数
不知道为什么我会收到错误消息。有什么想法吗?
这是我目前正在处理的代码:
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>
</>
);
}
}
【问题讨论】:
-
api 调用的响应是什么?