Set up React Web SDK

Install the package

$yarn add @launchdarkly/react-sdk

Initialize the SDK

React Web SDK initialization
1// Add the code below to the root of your React app.
2import { createRoot } from 'react-dom/client';
3import { createLDReactProvider, LDContext } from '@launchdarkly/react-sdk';
4
5function App() {
6 return <div>Let your feature flags fly!</div>
7}
8
9// A "context" is a data object representing users, devices, organizations, and other entities.
10const context: LDContext = {
11 kind: 'user',
12 key: 'EXAMPLE_CONTEXT_KEY',
13 email: 'biz@face.dev',
14};
15
16// This is your client-side ID.
17const LDReactProvider = createLDReactProvider('YOUR_CLIENT_SIDE_ID', context);
18
19createRoot(document.getElementById('root') as HTMLElement).render(
20 <LDReactProvider>
21 <App />
22 </LDReactProvider>,
23);

You can find your server-side SDK keys, mobile keys, and client-side ID on the SDK keys page under Settings.

To learn more, read Initialize the client in the React Web SDK reference guide.