api - 我如何显示 API 结果
问题描述
如何在 React 中显示以下 API 结果?
我用来从 url 获取 api 数据的代码
constructor(props) {
super(props);
this.state = { results: [] };
}
componentDidMount() {
fetch('url', {
method: 'GET',
headers: {
'api-key': 'api-key'
}
})
.then(results => results.json())
.then(data => this.setState({ results: data }));
}
但这给了我这条线的错误信息
TypeError:results.map 不是函数 Function.renderemployeeTable
我尝试用 this.state = { results: [] };
下面的 api 数据替换这一行,并能够使用<h3 class="title">{results.data.Table[0].first_name}{results.Table[0].last_name}</h3>
解决方案
您需要存储状态或将其作为道具和显示发送。
<div>{data.Table[0].first_name}</div>
推荐阅读
- c++ - 在 C++ 中,给定命名空间范围的 const float 依赖于全局 const float,它们的初始化顺序是否得到保证?
- android - 获取 RecyclerView 中的每个 FrameLayout div 元素
- javascript - 如何在 JavaScript 中生成十六进制二进制数据的 sha256 哈希?
- javascript - IE - 如何在提交表单后禁用输入验证检测?
- python - 我不能使用 celery 任务
- python - 我希望它在不是 0 或 1 时给我错误
- amazon-web-services - 如何确保每个 aws lambda 的不同公共 IP
- c++ - 在 Release 中作为 QImage 加载时,相同的图像具有不同的像素数据
- oracle - 使用 Jenkins 构建和部署 Oracle Forms 10g?
- android - 无法从 Ubuntu 18.10 访问 Redmi Note 5 Pro