1

I am trying du use react-native-contacts with a react-native app made with Expo, but I have this error message :

undefined is not an object (evaluating '_reactNativeContacts.default.getAll')

Here is the code I use :

import React from 'react';
import {
  Image,
  Platform,
  ScrollView,
  StyleSheet,
  Text,
  TouchableOpacity,
  View,
  Modal,
  TouchableHighlight,
  ImageBackground,
  TextInput,
  Picker,
  PermissionsAndroid
} from 'react-native';
import { WebBrowser } from 'expo';
import Contacts from 'react-native-contacts';

import { MonoText } from '../components/StyledText'; 


  Contacts.getAll((err, contacts) => {
    if (err === 'denied'){
      // error
    } else {
      // contacts returned in Array
    }
  })

I tried to follow all he steps for the installation in this page for the android part : https://github.com/rt2zz/react-native-contacts#getting-started

But I don't find where I can do this part : I don't know where I can find this file : android/settings.gradle

By the way I tried this command "react-native link" in my app directory and nothing changed.

Android
In android/settings.gradle
...
include ':react-native-contacts'
project(':react-native-contacts').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-contacts/android')
In android/app/build.gradle
...
dependencies {
    ...
    implementation project(':react-native-contacts')
}

Has anyone had this kind of problem ? Thanks for help !

Neiluj
  • 113
  • 12

2 Answers2

2

As far as I understand, you are developing your app with Expo. Some of independent libraries doesn't work well with Expo. I have two suggestions for you.

  1. If you want to keep using react-native-contacts, you need to eject your app from Expo
  2. Or directly use Expo's contacts api, You can find the details in this link Expo's Contacts I would do this which is less work for you to do and solve your problem

    import { Contacts } from 'expo';
    
    const { data } = await Contacts.getContactsAsync({
        fields: [Contacts.Fields.Emails],
    });
    
    if (data.length > 0) {
        const contact = data[0];
        console.log(contact);
    }
    

You can find same issue created in react-native-contacts github page . Issue

sinan
  • 570
  • 5
  • 24
  • Thanks a lot ! That's prettay anoying not to be able to use other librairies :-/ Do you know a way to ski expo ? – Neiluj Apr 24 '19 at 13:22
  • 1
    You need to eject your expo project by `expo eject` . I wouldnt recommend you to eject if you dont need to write native codes. You can read here in details. https://docs.expo.io/versions/v32.0.0/expokit/expokit/ – sinan Apr 25 '19 at 06:22
  • Thanks ! I ll go with expo for the moment ! Thanks a lot ! – Neiluj Apr 25 '19 at 12:09
0

July 2021 update

The Contacts module has been moved from the core expo package to expo-contacts (see documentation).

Example:

import * as Contacts from 'expo-contacts';

const { status } = await Contacts.requestPermissionsAsync();

if (status === 'granted') {
  const { data: contacts } = await Contacts.getContactsAsync();

  console.log('Retrieved contacts!', contacts);
}
Yulian
  • 6,262
  • 10
  • 65
  • 92