reactjs - TypeError:Events.map 不是函数
问题描述
所以我正在使用reactjs,但是当我想用componentdidMount调用我的api时,我在reactjs中很新
class Rank extends Component {
constructor(props){
super(props);
this.state = {Events: {}, golfer_events: [] }
}
componentDidMount(){
fetch('http://localhost/Golfer/api/EventApi/rank?id=2')
.then(response =>{
return response.json()
})
.then(json => console.log("json:", json))
.then(json => this.setState({Events: json.conversation}))
}
render() {
const {Events} = this.state;
return (
<div>
{
Events.map(event =>{
const {id, title, golfer} = event;
return(
<div key ={id}>
<Card>
<CardBody>
{title}
{golfer}
</CardBody>
</Card>
</div>
)
})
}
</div>
);
}
}
解决方案
在构造函数中,您将事件设置为对象而不是数组。对象没有导致问题的地图功能。
constructor(props){
super(props);
this.state = {Events: [] = [], golfer_events: [] = [] }
}
componentDidMount(){
fetch('http://localhost/Golfer/api/EventApi/rank?id=2')
.then(response =>{
return response.json()
})
.then(json => console.log("json:", json))
.then(json => this.setState({Events: (json.conversation || [])}))
}
推荐阅读
- opengl - 尝试在 OpenGL 中使用三角形绘制复杂多边形时出错
- ffmpeg - 为什么会出现 ffmpeg 错误/警告:过去的持续时间 0.933205 太大?将桌面录制为视频文件时会得到很多
- javascript - 如何从承诺(异步)中提取数组以在同步代码中使用?
- arrays - Angular:如何处理数组中的外键
- mysql - 获取 SQL 错误“每个派生表都必须有自己的别名 sql”
- html - 如何使网页的打印视图缩放以始终适合单个物理页面?
- python - 使用 Python 3 和 Cloud NDB 的 msgprop.EnumProperty 和 messages.Enum 的最佳实践是什么?
- c# - 从 Razor 页面发布会清除模型值 - 什么时候不应该?
- java - 我正在尝试使用 Apache POI 库读取 excel 文件,但是当我尝试读取单元格类型时,我得到一个空指针异常
- react-native - 位置错误“谷歌播放服务不可用”,反应原生