javascript - 我在 JSX 反应组件中获取数据时遇到问题
问题描述
我想从 swapi 获取数据并用这些数据做 jsx 组件,但是当我想使用这些数据时反应给我一个错误这是我的获取
var promise = await fetch('https://swapi.dev/api/people/1/')
.then((res) => res.json() )
.then(data => setLuke(data.data))
这是反应错误:
对象作为 React 子级无效(找到:[object Promise])。如果您打算渲染一组子项,请改用数组。 我不知道该怎么办你能帮我解决这个小问题吗
解决方案
You are probably rendering your promise var instead of {luke}. Also note that data will be a json object so you won't be able to render it directly. You will need to stringify it first. data.data which you are currently using will also be undefined. Just use data, it already contains the json.
await fetch("https://swapi.dev/api/people/1/")
.then(res => res.json())
.then(data => {
setLuke(JSON.stringify(data));
});
You should then be able to render it using {luke}
推荐阅读
- docker - 在heroku容器登录中获得权限被拒绝
- ruby-on-rails - 无法在 Big Sur 上安装 rdiscount 1.6.8
- javascript - r如何强制在 javascript 执行期间立即呈现网页?
- c# - c# 应用程序无法在 Win Server 2016 上运行
- c - 为什么在 C 中调用 free 时出现 3221226356 错误?
- c++ - 如何使用 Cuda 将动态特征向量传递给 GPU?
- sql - 将 PostgreSQL 导出为 json
- python-3.x - 使用 Pandas DataFrame 中的数据创建嵌套字典
- node.js - 无法使用 firebase 导出条带 API
- macos - MacOS - mdfind:如何从给定文件的内容角度查找相同的文件?