【发布时间】:2019-03-21 10:05:13
【问题描述】:
我有以下从 AsyncStorage 中检索的对象,我想在 FlatList 中显示它,但我收到以下错误:
Invariant Violation: Invariant Violation: Invariant Violation: Invariant Violation: Tried to get frame for out of range index NaN
我的 JSON 格式是:
Object {
"44": Object {
"pro_id": "44",
"pro_img": "url",
"pro_name": " S4 ",
},
"52": Object {
"pro_id": "52",
"pro_img": "url",
"pro_name": " S4 ",
},
}
上面的 JSON 是从 AsyncStorage 检索到的,如下所示:
retrieveData = async () => {
try {
await AsyncStorage.getItem('userFavorites')
.then(req => JSON.parse(req))
.then(json => this.setState({ favPro:json }))
} catch (error) {
}
}
我在FlatList 中渲染项目的方法是这样定义的:
fav = ({ item }) => {
return (
<View>
<Text>{item.pro_id+ item.pro_name}</Text>
</View>
)
}
最后,我将FlatList渲染如下:
<FlatList
data={this.state.favPro}
renderItem={this.fav}
keyExtractor={item => item.pro_id}
/>
如何使用FlatList 组件显示我的 JSON 数据?
【问题讨论】:
-
Invariant Violation: Invariant Violation: Invariant Violation: Invariant Violation: 试图获取超出范围索引 NaN 的帧
标签: javascript reactjs react-native