react-native-contacts returns undefined

怎甘沉沦 提交于 2021-02-09 10:50:47

问题


I'm tryna get the contacts list of the device but react-native-contacts returns undefined.

I've Installed the package and linked it as mentioned in the project's GitHub page https://github.com/rt2zz/react-native-contacts

I've also added the permissions to the android-manifest file. How do I get it works


回答1:


This happens mostly because of the linking-step was missing as there are native modules required:

After npm install react-native-contacts (or yarn add) you are required to link the stuff with

react-native link react-native-contacts

Afterwards you need to rebuild your project (react-native run-android or build in Android Studio). Just to "Reload" in Simulator or on Device is not sufficient after linking a native library.

Also some more permissions needs to be placed in AndroidManifest.xml:

<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />



回答2:


My stack :

  • react-native: 0.45.1
  • react-native-contacts: ^1.0.0"

I was still getting the error after linking the module as specified above. Finally resolved the issue after adding the following to ReactNativeContacts.java (/node_modules/react-native-contacts/android/src/main/java/com/rt2zz/reactnativecontacts):

import com.facebook.react.bridge.JavaScriptModule;

@override public List<Class<? extends JavaScriptModule>> createJSModules() { return Collections.emptyList(); }


来源:https://stackoverflow.com/questions/45269890/react-native-contacts-returns-undefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!