【发布时间】:2021-04-08 18:15:14
【问题描述】:
我目前正在学习react native,并探索在列表视图中渲染api数据的过程。现在我已经有了 api 数据,但是当我尝试映射它说的数组时。
TypeError:this.state.readings.map 不是函数。 (在'this.state.readings.map(函数(值){ 返回控制台.log(值); })', 'this.state.readings.map' 未定义)
这里是示例 api:
在我的 ComponentDidMount 内部:
componentDidMount() {
fetch('http://myip:3000/api/readings/3')
.then(response => response.json())
.then(data => this.setState({readings:JSON.stringify(data)}))
}
我的州:
this.state = {
readings:[],
}
我的渲染:
render() {
console.log(this.state.readings);
return (
{
this.state.readings.map(value => {
return (
console.log(value)
)
})
}
)
}
【问题讨论】:
-
如果您可以将示例添加到代码沙箱中,这将有所帮助。
标签: reactjs react-native