Review the JavaScript SDK 3.x to 4.0 migration guide tooThe React Web SDK is based on
@launchdarkly/js-client-sdk which is our JavaScript SDK at version 4.0.0 and higher.To learn about the differences between that version and the older versions of the JavaScript SDK, read the JavaScript SDK 3.x to 4.0 migration guide. Those changes are also relevant to the React SDK.Prerequisites
To complete this migration, you must have the following prerequisites:- A LaunchDarkly account.
- React version 18.0.0 or higher. We recommend using React 19 if possible.
- React Web SDK version 3.x installed.
@launchdarkly/react-sdk package, but it is not officially supported.
Understanding what changed in version 4.0
Introducing support for React Server ComponentsThis update includes support for React Server Components. That functionality is included in the React Web SDK. To learn more, read React Server Component support.
- Named contexts
useInitializationStatusreplacesuseLDClientErroruseInitializationStatusreplacesasyncWithLDProvider- Removed higher-order components:
withLDProviderandasyncWithLDProvider - Changed deferred initialization behavior
- Bootstrap is now a top-level option
- Deprecations
- Removed APIs
Named contexts
Version 4.0 supports custom React contexts with thereactContext option on createLDReactProvider and through an optional argument on every hook.
Here’s an example:
useInitializationStatus replaces useLDClientError
The old useLDClientError only returned an error when the client failed to initialize. The new useInitializationStatus() provides both the full initialization state and the error.
useInitializationStatus tells you the full state of initialization, not only if there was an error. Use status to gate rendering and error for error reporting.
useInitializationStatus replaces asyncWithLDProvider
The old function blocked rendering until the client initialized. In version 4, you can use useInitializationStatus to conditionally render loading or error states.
Removed higher-order components: withLDProvider and asyncWithLDProvider
We’ve removed our higher-order components as the React ecosystem now heavily favors the hooks pattern.
We strongly recommend migrating to use our createLDReactProvider for client instantiation.
However, if you prefer to continue using HOCs, you can create a HOC using our alternative
createClient.
createClient does not automatically call the start() method that kicks off the SDK
client initialization.
Changed deferred initialization behavior
deferInitialization has changed option location, its default value, how it behaves when set to true, and how it starts manually.
| Initialization subject | React Web SDK v3.x | React Web SDK v4.0 |
|---|---|---|
| Option location | ||
| Default | false (auto-initialize on mount) | false (auto-start before provider mounts) |
When true | Provider does not call initialize(); user calls ldClient.identify(context) to start. | Factory does not call start(); user must call client.start() manually. |
| How to start manually | ldClient.identify(context). Identify doubled as init in the old SDK | client.start(). An explicit start is required. identify() switches context only after start() |
createLDReactProviderWithClient always behaves as if deferInitialization: true. It never calls start() automatically.
Bootstrap is now a top-level option
In v3.x, bootstrap data was nested insideoptions. In v4.0, bootstrap is a top-level option on createLDReactProvider.
The bootstrap data format is unchanged. You can pass either a plain key-value object, such as { 'example-flag-key': true }, or the output of allFlagsState().toJSON(), which includes $flagsState and $valid metadata.
startOptions.bootstrap. The top-level bootstrap option is merged into startOptions.bootstrap when the client starts. If you provide both, the top-level value takes precedence.
Deprecations
useFlags is available, but deprecated. We strongly recommend using typed variation hooks for flag evaluations instead.
If you must, you can call allFlags from the LDClient. Here’s how:
Removed APIs
The following APIs have been removed:useLDClientError(), replaced withuseInitializationStatus().error.withLDProvider(), replaced withcreateLDReactProvider().asyncWithLDProvider(), replaced withcreateLDReactProvider().withLDConsumer(), replaced withuseLDClient(), and typed variation hooks such asuseBoolVariation(),useStringVariation(),useNumberVariation(),useJsonVariation().LDProvidercomponent, replaced withcreateLDReactProvider().reactSdkContextFactory(), replaced withinitLDReactContext(). To learn more, read Named contexts, above.camelCaseKeys()utility function. Camel-case key conversion is no longer exposed as a standalone helper. The typed variation hooks always use the original flag key. TheuseFlagshook still applies camel-casing internally and can be opted out of withldOptions.useCamelCaseFlagKeys: false.
Updating your dependencies
@launchdarkly/js-client-sdk is bundled as a transitive dependency of @launchdarkly/react-sdk. You do not need to install or update it manually.