[英]How to convert array of object to Flatlist data - React Native
I want to display a flatlist of all the phone's contacts. 我想显示所有电话联系人的清单。 Now I have an Array of objects but the flatlist doesn't works with objects.
现在,我有一个对象数组,但平面列表不适用于对象。 I tried a lot of things found on the web but in vain...
我尝试了很多在网上找到的东西,但是徒劳无功...
I still get this error : Invariant violation: Tried to get frame for out of range index NaN. 我仍然收到此错误:不变违规:试图获取超出范围索引NaN的帧。
What I have now is : Array [ Object { "name": "toto", "id": 550, ... }, ... ] 我现在所拥有的是:Array [Object {“ name”:“ toto”,“ id”:550,...},...]
What I would like is : Array [ { name: "toto", id: 550, ... }, ... ] 我想要的是:Array [{name:“ toto”,id:550,...},...]
My code : 我的代码:
async getContactAsync() {
// permissions
// Get all contacts from device
const { data: contacts } = await Contacts.getContactsAsync({
fields: [
Contacts.Fields.id,
Contacts.Fields.name,
Contacts.Fields.firstName,
Contacts.Fields.lastName,
Contacts.Fields.phoneNumbers
]
});
if (contacts.length > 0) {
return contacts;
}
}
keyExtractor = (item, index) => index.toString()
renderItem = ({ item }) => (
<ListItem
title={item.name}
...
}
/>
)
render () {
return (
<FlatList
keyExtractor={this.keyExtractor}
data={this.getContactAsync()}
renderItem={this.renderItem}
/>
)
}
can you try this inside your if state 你可以在if状态下尝试这个吗
if (contacts.length > 0){
return contacts.map(item => {...item})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.