问题
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