LaunchDarkly React Client SDK
The `launchdarkly-react-client-sdk` is the official client-side SDK for integrating LaunchDarkly feature flags into React applications. It leverages React's Context API and Hooks to provide a convenient way to access flag values and the LaunchDarkly client instance throughout a component tree. The current stable version is 3.9.0. This SDK typically sees several releases per year, often driven by updates to the underlying `js-client-sdk` and new React features. Key differentiators include its tight integration with React's component lifecycle, providing hooks like `useFlags` and `useLDClient`, and simplifying prop drilling for feature flags. It's designed for modern React development, compatible with React 16.3.0+, with Hooks requiring 16.8.0+.
Common errors
-
Error: React hooks can only be called inside of the body of a function component. This could happen for one of the following reasons: 1. You might have mismatching versions of React and the renderer (such as React DOM) 2. You might be breaking the Rules of Hooks
cause Attempting to use `useFlags` or `useLDClient` outside a functional React component or with an incompatible React version.fixEnsure `useFlags` and `useLDClient` are only called within functional React components. Verify `react` and `react-dom` versions are 16.8.0 or higher. -
LaunchDarkly: clientSideId must be specified.
cause The `clientSideId` property was not provided or was an empty string during the initialization of `withLDProvider` or `asyncWithLDProvider`.fixEnsure `clientSideId` is correctly passed to `withLDProvider` or `asyncWithLDProvider`, typically loaded from environment variables (e.g., `process.env.REACT_APP_LD_CLIENT_SIDE_ID`). -
Cannot read properties of undefined (reading 'variation')
cause Attempting to access a flag value from the `ldClient` or `useFlags` before the SDK has fully initialized or if the flag is not found/enabled for client-side SDKs.fixEnsure the SDK is fully initialized by checking `isInitialized` from the provider or by using the `waitForInitialization` option. Also, verify the flag exists and is enabled for client-side SDKs in the LaunchDarkly dashboard.
Warnings
- gotcha The React SDK requires a 'Client-side ID', not an 'SDK key' or 'Mobile key', for initialization. Using the wrong key will prevent the SDK from connecting.
- gotcha For any feature flag to be accessible in the client-side React SDK, the 'Make this flag available to client-side SDKs' checkbox must be enabled on the flag's settings page in the LaunchDarkly dashboard.
- breaking Using React Hooks (e.g., `useFlags`, `useLDClient`) or `asyncWithLDProvider` requires React version 16.8.0 or later. Earlier versions of React will result in runtime errors.
- breaking From `launchdarkly-react-client-sdk` v3.3.2 onwards, the build target for ESBuild was explicitly set to ES2015 to ensure broader browser compatibility, overriding potential `tsconfig.json` defaults.
Install
-
npm install launchdarkly-react-client-sdk -
yarn add launchdarkly-react-client-sdk -
pnpm add launchdarkly-react-client-sdk
Imports
- withLDProvider
const { withLDProvider } = require('launchdarkly-react-client-sdk');import { withLDProvider } from 'launchdarkly-react-client-sdk'; - useFlags
import { useFlags } from 'launchdarkly-react-client-sdk'; - useLDClient
import { useLDClient } from 'launchdarkly-react-client-sdk'; - asyncWithLDProvider
import { asyncWithLDProvider } from 'launchdarkly-react-client-sdk';
Quickstart
import React from 'react';
import { withLDProvider, useFlags } from 'launchdarkly-react-client-sdk';
const clientSideId = process.env.REACT_APP_LD_CLIENT_SIDE_ID ?? '';
if (!clientSideId) {
console.error('LaunchDarkly Client-side ID is not set. Please set REACT_APP_LD_CLIENT_SIDE_ID in your environment variables.');
}
interface MyComponentProps {
// any props for your component
}
const MyFeatureComponent: React.FC<MyComponentProps> = () => {
const { myFeatureFlag, anotherFlag } = useFlags();
return (
<div>
<h1>My Application</h1>
{myFeatureFlag ? (
<p>Feature is ON! Displaying content for the feature.</p>
) : (
<p>Feature is OFF. Default content.</p>
)}
{anotherFlag && <p>Another flag is also ON!</p>}
</div>
);
};
const MyWrappedComponent = withLDProvider({
clientSideId: clientSideId,
user: { key: 'example-user', anonymous: true },
options: {
evaluationReasons: true
}
})(MyFeatureComponent);
export default MyWrappedComponent;