React Native Facebook SDK Next
react-native-fbsdk-next is a community-maintained fork providing Facebook SDK integration for React Native applications. It wraps the native iOS and Android Facebook SDKs, exposing their functionalities entirely through documented JavaScript modules, eliminating the need for direct native code interaction. The package aims to continue support and upgrades after Facebook ceased official maintenance of the original `react-native-fbsdk`. Currently at version 13.4.3, it receives frequent updates to align with the latest native Facebook SDK versions and React Native ecosystem changes. It differentiates itself by its active community support, cross-platform compatibility from a single package, and continuous integration of new features and bug fixes from upstream Facebook SDKs.
Common errors
-
Cocoapods could not find compatible versions for pod 'react-native-fbsdk-next'
cause Your Cocoapods version is too old, or there's a version mismatch with the native Facebook iOS SDK requirements.fixRun `pod install --repo-update` in your `ios/` directory. Ensure your Cocoapods version is 1.12 or higher. Update your `Podfile` if necessary to explicitly specify `platform :ios, '13.0'` or higher. -
Error: RNFBDK (or similar native module name) doesn't exist. Are you sure you've linked all the native dependencies and set up your project correctly?
cause Native modules not correctly linked, either due to autolinking failure or manual linking errors.fixFor React Native 0.60+, ensure autolinking is working correctly. Run `npx react-native link react-native-fbsdk-next` if needed, then `cd ios && pod install`, and rebuild your project. Double-check `AndroidManifest.xml` and `Info.plist` for correct SDK configuration. -
Manifest merger failed : Attribute application@appComponentFactory value=(...) from [...] AndroidManifest.xml:23:18-86 is also present at [...] AndroidManifest.xml:24:18-86 value=(...). Suggestion: add 'tools:replace="android:appComponentFactory"' to <application> element at AndroidManifest.xml:22:5-24:19 to override.
cause Conflicting `appComponentFactory` declarations in your Android manifest, often when integrating multiple libraries that target newer Android features.fixAdd `tools:replace="android:appComponentFactory"` inside the `<application>` tag in your `android/app/src/main/AndroidManifest.xml` file. Ensure you also add `xmlns:tools="http://schemas.android.com/tools"` to your root manifest tag. -
Cannot read property 'logInWithPermissions' of undefined
cause The `LoginManager` (or other module) is `undefined` at runtime, indicating the native module was not properly initialized or linked, or the JavaScript bridge failed.fixVerify that you have correctly completed all installation steps, including `cd ios && pod install` and rebuilding both iOS and Android projects. Confirm that the app ID and client token are correctly configured in your native project files (Info.plist, AndroidManifest.xml).
Warnings
- breaking Version 13.0.0 introduced significant breaking changes. Android applications may experience issues due to upstream Facebook SDK changes related to integrity policy enforcement, potentially blocking traffic for policy-violating apps. Additionally, Cocoapods version >= 1.12 is now a strict requirement for iOS due to the adoption of privacy manifests, and iOS login/limited login functionality has changed, requiring review of the Facebook Developer blog post from March 28, 2024.
- gotcha The library has specific React Native version compatibility. Versions of `react-native-fbsdk-next` after 4.2.0 officially support React Native versions `>=0.63.3`. While older RN versions might technically work, they are not supported, and future updates may introduce breaking changes without strict adherence to semantic versioning for these unsupported older environments.
- gotcha When integrating with Facebook Android SDK v18, there can be an `AD_SERVICES_CONFIG` manifest merge collision. This was addressed in `react-native-fbsdk-next` v13.4.1 but could still affect projects using older versions or specific build configurations.
- gotcha The package requires Node.js version >=18. Using older Node.js versions may lead to build errors or unexpected behavior during installation and development.
Install
-
npm install react-native-fbsdk-next -
yarn add react-native-fbsdk-next -
pnpm add react-native-fbsdk-next
Imports
- LoginManager
const LoginManager = require('react-native-fbsdk-next').LoginManager;import { LoginManager } from 'react-native-fbsdk-next'; - AccessToken
import AccessToken from 'react-native-fbsdk-next/js/AccessToken';
import { AccessToken } from 'react-native-fbsdk-next'; - AppEventsLogger
const { AppEventsLogger } = require('react-native-fbsdk-next');import { AppEventsLogger } from 'react-native-fbsdk-next'; - Settings
import { Settings } from 'react-native-fbsdk-next';
Quickstart
import { LoginManager, AccessToken, AppEventsLogger, Settings } from 'react-native-fbsdk-next';
import React, { useEffect, useState } from 'react';
import { View, Text, Button, Alert, StyleSheet } from 'react-native';
const App: React.FC = () => {
const [loggedIn, setLoggedIn] = useState(false);
const [userName, setUserName] = useState<string | null>(null);
useEffect(() => {
// It's good practice to explicitly initialize the SDK.
// Ensure your app ID and client token are set in Info.plist (iOS) / AndroidManifest.xml (Android).
Settings.initializeSDK();
AccessToken.getCurrentAccessToken().then((data) => {
if (data) {
setLoggedIn(true);
console.log('User already logged in:', data.accessToken);
AppEventsLogger.logEvent('AppStartedWithExistingLogin');
} else {
setLoggedIn(false);
console.log('No existing Facebook access token.');
}
});
}, []);
const fbLogin = async () => {
try {
AppEventsLogger.logEvent('LoginButtonPressed');
const result = await LoginManager.logInWithPermissions(['public_profile', 'email']);
if (result.isCancelled) {
Alert.alert('Login cancelled');
console.log('Login cancelled');
} else {
Alert.alert('Login success with permissions: ' + result.grantedPermissions?.toString());
console.log('Login success result: ', result);
const data = await AccessToken.getCurrentAccessToken();
if (data) {
setLoggedIn(true);
console.log('Access Token:', data.accessToken);
AppEventsLogger.logEvent('FacebookLoginSuccess');
} else {
Alert.alert('Failed to get access token after login.');
console.log('Failed to get access token after login.');
}
}
} catch (error: any) {
Alert.alert('Login error: ' + error.message);
console.error('Login failed with error: ', error);
}
};
const fbLogout = () => {
LoginManager.logOut();
setLoggedIn(false);
setUserName(null);
AppEventsLogger.logEvent('FacebookLogout');
Alert.alert('Logged out from Facebook');
};
return (
<View style={styles.container}>
<Text style={styles.title}>Facebook SDK Integration Example</Text>
{loggedIn ? (
<>
<Text style={styles.statusText}>You are logged in to Facebook!</Text>
<Button title="Logout from Facebook" onPress={fbLogout} />
</>
) : (
<Button title="Login with Facebook" onPress={fbLogin} />
)}
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
padding: 20,
},
title: {
fontSize: 20,
marginBottom: 20,
textAlign: 'center',
},
statusText: {
marginBottom: 10,
fontSize: 16,
},
});
export default App;