reactjs - 我有一个 fetch ReactJS 的问题,我无法在组件安装或函数中获取数据
问题描述
我想从我的状态的电影数据库中获取数据,我尝试使用我做不到的钩子,现在与状态相同的问题。
componentDidMount() {
fetch(jsonUrl)
.then(response => {
response.json();
})
.then( data => {
console.log("CDM", data);
});
}
当我尝试在 ComponentDidMount 内部或函数内部或 useEffect Hooks 内部进行获取时,它不会返回任何内容,只会返回未定义的内容。
我要数据,请帮帮我。
非常感谢
解决方案
我看到了回调问题
.then(response => {
response.json();
// No explicit return then this function will return undefined
})
它应该是
.then(response => {
return response.json(); // this returns whatever returned from response.json()
})
或者
.then(response => response.json())
推荐阅读
- webrtc - 将视频从独立桌面应用程序(不是浏览器)流式传输到远程桌面应用程序(不是浏览器)
- asynchronous - 如何在异步事件期间暂停在后台运行的游戏?
- c# - 如何在asp.net c#的datalist控件中添加按钮A到Z?
- amazon-web-services - Route 53 主 DNS 主机名:主 DNS IP 地址
- c# - 修改位图的不可变方法
- python - 用于在所需的数组元素中插入空格的 Python 字符串修复
- linux - 每个关键字计算grep匹配?
- php - 过滤搜索结果 PHP
- c# - c# 使用运行时创建的listview
- c++ - 结构、模板使用和继承问题