2017-05-03 9 views
0

私の英語は悪いですが、私は私の問題を説明しようとします。コンポーネント固有の連絡先を持つ連絡先の小道具を取得

私はReact Native Androidで連絡先のgivenNameを取得しました。したがって

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> 
); 
} 
} 

、iは{}内の連絡先の名前を印刷する必要がある:I)コンポーネントは、ネイティブ・コンタクトを反応して(にconsole.logで印刷する次のコードを使用してのみ連絡先の名前をインストールしました。まるで平らな物のように私はそれを得ようとしました。ありがとう!

答えて

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}); 
    } 
    }) 
} 

レンダリングし、使用状態のうち、あなたのコードを移動する必要があります()あなたの答えのための

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

おかげのようになります。私はあなたの提案を試みたが、プロジェクトを実行している:未定義のプロパティ '0'を読み取ることができません。 –

+0

Contacts.getAll()が返される前にレンダリングしようとしているように聞こえますが、state.contactsのnullチェックがprobする必要がありますか? –

+0

OK、state.contacts == nullをチェックすると、コンソールにfalseが表示されます。したがって、状態は {...}に到着しませんか? –

関連する問題