React Facebook

FacebookProvider

Initializes the Facebook SDK and provides context to all child components and hooks.

FacebookProvider loads the Facebook JavaScript SDK, initializes it with your app configuration, and exposes a React context that every other component and hook in this library depends on. Place it at the root of the subtree that needs Facebook features.

Import

import { FacebookProvider } from 'react-facebook';

Props

FacebookProvider extends FacebookOptions with children and an optional pixelId.

PropTypeDefaultDescription
appIdstringrequiredYour Facebook App ID.
childrenReactNoderequiredChild elements that will have access to the Facebook context.
pixelIdstringundefinedWhen provided, wraps children with FacebookPixelProvider automatically. For advanced pixel options, use FacebookPixelProvider directly.
versionstring'v23.0'Graph API version string.
languagestring'en_US'SDK locale code (e.g. 'fr_FR', 'de_DE'). Changing this at runtime reloads the SDK.
lazybooleanfalseWhen true the SDK script is not injected until a component or hook calls init().
cookiebooleanfalseEnable cookie support so your server can access the session.
statusbooleanfalseCheck login status when the SDK initializes.
xfbmlbooleanfalseParse XFBML tags automatically on init.
frictionlessRequestsbooleanfalseEnable frictionless requests for game apps.
debugbooleanfalseLoad the debug build of the SDK (sdk/debug.js).
chatSupportbooleanfalseLoad the Customer Chat SDK variant.
autoLogAppEventsbooleantrueAutomatically log app events to Facebook Analytics.
domainstring'connect.facebook.net'Domain from which the SDK script is loaded.

Usage

Basic

import { FacebookProvider } from 'react-facebook';

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

With All Options

<FacebookProvider
  appId="YOUR_APP_ID"
  version="v23.0"
  language="en_US"
  lazy={false}
  cookie={false}
  status={false}
  xfbml={false}
  debug={false}
  autoLogAppEvents={true}
>
  {children}
</FacebookProvider>

With Facebook Pixel

Pass a pixelId to enable Pixel tracking without adding a separate FacebookPixelProvider. For advanced pixel options (debug, autoConfig, advancedMatching), use FacebookPixelProvider directly.

<FacebookProvider appId="YOUR_APP_ID" pixelId="YOUR_PIXEL_ID">
  {children}
</FacebookProvider>

Lazy Initialization

When lazy is true, the SDK will not load until a child component or hook triggers init(). This is useful for deferring the network request until the user actually interacts with a Facebook feature.

<FacebookProvider appId="YOUR_APP_ID" lazy>
  {/* SDK won't load until Login, Like, or another component needs it */}
  {children}
</FacebookProvider>

On this page