Sergey B
Sergey B

Reputation: 1

Can't import Contacts from 'react-native-contacts'

I have already tried to solve this problem using

react-native-contacts getAll return null, react-native-contacts returns undefined.

But, unfortunately, I can't get success. I've already run below shells:

npm install react-native-contacts --save

react-native link react-native-contacts

Here is my code =========

import Contacts from 'react-native-contacts';

componentDidMount() {
    if(Platform.OS === 'ios'){
      Contacts.getAll((err, contacts) => {
        if (err) {
          throw err;
        }
        // contacts returned
        console.log("contacts => ", contacts);
      })
    }else if(Platform.OS === 'android'){
      PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.READ_CONTACTS,
        {
          title: 'Contacts',
          message: 'This app would like to view your contacts.'
        }
      ).then(() => {
        Contacts.getAll((err, contacts) => {
          if (err === 'denied'){
            // error
          } else {
            // contacts returned in Array
            console.log("contacts => ", contacts);
          }
        })
      })
    }
  }

====================================== Here is my result.

screen shoot my error terminal

My environment :

> expo --version
> 4.0.6
> node --version
> v14.15.0
> npm --version
> 6.14.8

Upvotes: 0

Views: 740

Answers (1)

Belgin Android
Belgin Android

Reputation: 307

As You Are Using Expo You Can Try Using The Contacts Module From Expo . The Documentation For Contacts https://docs.expo.io/versions/v32.0.0/sdk/contacts/ .

In Case You Want To Use react-native-contacts You must eject expo and continue with react-native-cli

import { Contacts } from 'expo';

const { data } = await Contacts.getContactsAsync({
    fields: [Contacts.Fields.Emails],
});

if (data.length > 0) {
    const contact = data[0];
    console.log(contact);
}

Upvotes: 1

Related Questions