使用组件react-native-contacts

时间:2017-05-03 16:13:27

标签: react-native

我的英语不好,但我会尝试解释我的问题。

我在React Native Android中获得了prop的联系人名称。我安装了组件react-native-contacts并使用下一个代码打印console.log()只有联系人的姓名:

import React, { Component } from 'react';
...
import Contacts from 'react-native-contacts';
...
class myComponent extends Component {
...
 render() {
  Contacts.getAll((err, contacts) => {
   console.log(typeof err + err);
   if(err === 'denied'){
    // x.x
   } else {
    console.log(contacts);
    console.log("Nombre de contacto: " + contacts[0].givenName);
   }
  })
  return (
   <Container>
    ...
    <View>
      <Text>{}</Text>
    </View>
   </Container>
  );
 }
}

所以,我需要在{}中打印联系人的姓名。我试图让它好像是一个扁平物体。感谢的!

1 个答案:

答案 0 :(得分:0)

您应该将代码移出渲染并使用状态

componentWillMount() {
  Contacts.getAll((err, contacts) => {
    console.log(typeof err + err);
    if(err === 'denied'){
    // x.x
    } else {
      console.log(contacts);
      console.log("Nombre de contacto: " + contacts[0].givenName);
      this.setState({contacts}); 
   }
  })
}

然后你的render()就像

<View>
  <Text>{this.state.contacts[0].givenName}</Text>
</View>