React Facebook

Getting Started

Install react-facebook, set up the FacebookProvider, and render your first Facebook component.

Installation

Install the package with your preferred package manager:

npm install react-facebook
yarn add react-facebook
pnpm add react-facebook

Basic Setup

Wrap your application (or the subtree that needs Facebook features) with FacebookProvider. Every other component and hook in this library reads from the context that FacebookProvider creates.

import { FacebookProvider } from 'react-facebook';

export default function App({ children }) {
  return <FacebookProvider appId="YOUR_APP_ID">{children}</FacebookProvider>;
}

Provider Options

FacebookProvider accepts every property from the FacebookOptions type plus an optional pixelId.

PropTypeDefaultDescription
appIdstringrequiredYour Facebook App ID.
versionstring'v23.0'Graph API version.
languagestring'en_US'SDK locale (e.g. 'fr_FR', 'de_DE').
lazybooleanfalseWhen true, the SDK is not loaded until a component or hook calls init().
cookiebooleanfalseEnable cookie support for server access tokens.
statusbooleanfalseCheck login status on SDK init.
xfbmlbooleanfalseParse XFBML tags on init.
frictionlessRequestsbooleanfalseEnable frictionless requests.
debugbooleanfalseLoad the debug version of the SDK.
chatSupportbooleanfalseLoad the Customer Chat SDK variant.
autoLogAppEventsbooleantrueAutomatically log app events.
domainstring'connect.facebook.net'SDK domain.
pixelIdstringundefinedWhen provided, wraps children with FacebookPixelProvider automatically. For advanced pixel options, use FacebookPixelProvider directly.
<FacebookProvider appId="YOUR_APP_ID" language="en_US" lazy={false} pixelId="YOUR_PIXEL_ID">
  {children}
</FacebookProvider>

Your First Login Component

Once the provider is in place you can drop in any component. Here is a minimal login example:

import { FacebookProvider, Login } from 'react-facebook';

function App() {
  return (
    <FacebookProvider appId="YOUR_APP_ID">
      <Login
        onSuccess={(response) => {
          console.log('Logged in:', response.authResponse.accessToken);
        }}
        onError={(error) => {
          console.error('Login failed:', error);
        }}
        scope={['public_profile', 'email']}
      >
        Login with Facebook
      </Login>
    </FacebookProvider>
  );
}

Complete Auth Flow

Here is a realistic example combining hooks for a full login-to-profile-to-logout flow — the most common use case:

import { FacebookProvider, useLogin, useProfile, useLoginStatus } from 'react-facebook';

function AuthFlow() {
  const { status } = useLoginStatus();
  const { login, logout, loading: isLoggingIn } = useLogin();
  const { profile, loading: isLoadingProfile } = useProfile(['name', 'email', 'picture']);

  if (status === 'connected' && profile) {
    return (
      <div className="flex items-center gap-4">
        <img src={profile.picture?.data?.url} alt={profile.name} className="w-10 h-10 rounded-full" />
        <div>
          <p className="font-semibold">{profile.name}</p>
          <p className="text-sm text-gray-500">{profile.email}</p>
        </div>
        <button
          onClick={() => logout()}
          disabled={isLoggingIn}
          className="ml-auto text-sm text-red-600 hover:underline"
        >
          {isLoggingIn ? 'Logging out...' : 'Logout'}
        </button>
      </div>
    );
  }

  return (
    <button
      onClick={async () => {
        try {
          const response = await login({ scope: 'email,public_profile' });
          // Send token to your server
          await fetch('/api/auth/facebook', {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({
              accessToken: response.authResponse.accessToken,
              userID: response.authResponse.userID,
            }),
          });
        } catch (err) {
          console.error('Login failed:', err);
        }
      }}
      disabled={isLoggingIn}
      className="bg-[#1877F2] text-white px-6 py-3 rounded-lg font-semibold hover:bg-[#166FE5] disabled:opacity-50"
    >
      {isLoggingIn ? 'Connecting...' : 'Continue with Facebook'}
    </button>
  );
}

function App() {
  return (
    <FacebookProvider appId="YOUR_APP_ID">
      <AuthFlow />
    </FacebookProvider>
  );
}

Hooks Overview

Every component in this library has a corresponding hook for programmatic control. Hooks must be called inside a FacebookProvider.

HookPurpose
useLoginTrigger the login dialog, log out, and receive a LoginResponse.
useProfileFetch profile fields for the logged-in user.
useLoginStatusCheck the current authentication status.
useGraphAPIMake typed Graph API calls with loading/error states.
useShareOpen the Share dialog programmatically.
useFeedPost to a user's feed via the Feed dialog.
useSendSend a private message with a link via the Send dialog.
useFacebookDirect access to the low-level FacebookInstance and parse function.
useLocaleRead and change the SDK locale at runtime.
useSubscribeSubscribe to Facebook SDK events with automatic cleanup.
usePixelTrack standard and custom Pixel events.
usePageViewAutomatically fire a PageView event on mount or route change.

Pixel Quick Setup

You can enable Facebook Pixel tracking with zero extra components by passing pixelId to FacebookProvider:

import { FacebookProvider, usePixel } from 'react-facebook';

function App() {
  return (
    <FacebookProvider appId="YOUR_APP_ID" pixelId="YOUR_PIXEL_ID">
      <TrackingExample />
    </FacebookProvider>
  );
}

function TrackingExample() {
  const { track, pageView } = usePixel();

  return (
    <div>
      <button onClick={() => pageView()}>Track Page View</button>
      <button onClick={() => track('Purchase', { value: 29.99, currency: 'USD' })}>Track Purchase</button>
    </div>
  );
}

If you only need Pixel tracking without the full Facebook SDK, use FacebookPixelProvider directly. See the Pixel Provider documentation.

On this page