[英]React Native: Flatlist not working with json
This is my json object这是我的 json object
{"recipies" : [
{
"id": "1595929638-20",
"recipies": "Snickerdoodles",
"recipe name": "Snickerdoodles",
"imageUrl": "",
"servings": "Servings:",
"time": "30 mins",
"ingredients": "½ cup Butter, with salt",
"method": "Preheat oven to 400 degrees F (200 degrees C).",
}]
}
This how I have imported the json file and used the flatlist这就是我导入 json 文件并使用平面列表的方式
const customData = require('../Data/main.json');
const Food = () =>{
const renderItem = (itemData) =>{
return (
<Paper
title={itemData.item.recipies}
serves={itemData.item.servings}
time={itemData.item.time}
image={itemData.item.imageUrl}
/>
)
}
return(
<FlatList
bounces={true}
showsVerticalScrollIndicator={false}
data={customData}
numColumns={2}
keyExtractor={(item, index) => item.id}
renderItem={renderItem} />
)
}
export default Food
I am getting the error Invalid Violation: Tried to get frame for out of range index NaN
.Please can someone help me I am trying to output the data in my Paper
custom component which isn't causing the error it's the Flatlist我收到错误
Invalid Violation: Tried to get frame for out of range index NaN
。请有人帮助我我正在尝试 output 我的Paper
自定义组件中的数据,这不会导致错误,它是 Flatlist
You should change data props with customData.recipies您应该使用 customData.recipies 更改数据道具
data={customData.recipies}
Try this:尝试这个:
<FlatList
bounces={true}
key="portrait"
showsVerticalScrollIndicator={false}
keyExtractor={(item, index) => item.id}
data={customData.recipies}
numColumns={2}
renderItem={({item}) => renderItem(item)}
/>
Erhan Namal
was right in saying you need to change the value of the data
property to customData.recipies
, because Flatlist's data
property expects an array ( https://reactnative.dev/docs/flatlist.html#data ). Erhan Namal
说您需要将data
属性的值更改为customData.recipies
是正确的,因为 Flatlist 的data
属性需要一个数组( https://reactnative.dev/docs/flatlist.html#data )。
If you call renderItem
you need to actually pass the item.如果您调用
renderItem
您需要实际传递该项目。
And numColumns
needs a key property in order to work.并且
numColumns
需要一个关键属性才能工作。
Also there is no item
property in your json, so change renderItem
to this:您的 json 中也没有
item
属性,因此将renderItem
更改为:
const renderItem = (itemData) =>{
return (
<Paper
title={itemData.recipies}
serves={itemData.servings}
time={itemData.time}
image={itemData.imageUrl}
/>
)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.