reactjs - 我正在从 api 查询数据,但 useffect 呈现两次,所以我无法获取信息
问题描述
我成功地从 api 获取数据,但我无法通过它进行映射,因为在应用程序上首先呈现数据数组为空,然后显示 **data.map is not a function ** 的错误
const [data, setData] = useState([]);
const [names, setNames] = useState('')
useEffect(() => {
fetch(
`http://api.openweathermap.org/data/2.5/weather?q=kampala&appid=09da4ff6a7a9e25ddaa36518f64dc694`
)
.then((response) => response.json())
.then((response) => setData(response));
}, []);
console.log(data);
data.map(dt=>setName(dt.name))
解决方案
推荐阅读
- android - 下载图像并显示问题
- eslint - 我怎样才能让我使用标签更漂亮
- python - 可能性适合使用 scipy.stats 和 iminuit
- java - 当我从最近的图像中选择图像时,图像在 android | 中被破坏 最近的多张图片
- apache-spark - 我可以编写一个 Spark UDF 以使用 spark SQL 连接到 Oracle 数据库吗?
- python - 如何解决评估语句中的前导零错误(语法错误)
- python - Python `print` 函数中的 end='\r' 并不总是有效
- javascript - 使用任何脚本在 js 中的多个文件中生成现有函数的列表
- swift - 斯威夫特:为什么我的视图模型与我的视图中的切换不起作用?
- r - 改进 R 中的 Max 函数