This project is no longer maintained, as Launch Darkly has an official React SDK for web, React Native, and Gatsby.
Simple component helpers to support LaunchDarkly in your react app.
npm install --save react-launch-darkly
- React
v16.3.0
or greater- If you use an older version of React, you can continue to use version 1.4.0 of this library. However, we will no longer be actively maintaining version 1.x.
- LaunchDarkly client /
launchdarkly-js-client-sdk
launchdarkly-js-client-sdk
needs to be a dependency within the app usingreact-launch-darkly
- supported versions of
launchdarkly-js-client-sdk
:^2.0.0
(peer dependency)
To setup the LaunchDarkly
component wrapper, you'll probably want to include it in a top-level
layout component:
// MasterLayout.js
import React, { Component } from "react";
import { LaunchDarkly } from "react-launch-darkly";
export default class MasterLayout extends Component {
render () {
return (
<div>
<LaunchDarkly clientId={YOUR_LAUNCH_DARKLY_CLIENT_ID} user={{ key: "YOUR_USER_KEY" }}>
{this.props.children}
</LaunchDarkly>
</div>
);
}
}
Then in your lower-level components, to make use of the FeatureFlag
component:
// Home.js
import React, { Component } from "react";
import { FeatureFlag } from "react-launch-darkly";
export default class Home extends Component {
render () {
return (
<div>
<FeatureFlag
flagKey="home-test"
renderFeatureCallback={this._renderFeature}
/>
</div>
);
}
_renderFeature () {
return (
<div>Your new feature!</div>
);
}
}
- LaunchDarkly component
- FeatureFlag component
- SSR Support
- Overriding Feature Flags
- Identifying new users
Main component that initializes the LaunchDarkly js-client.
This is the client id that is provided to you by LaunchDarkly.
See the LaunchDarkly docs for more info.
Options that are passed to the LaunchDarkly JS client for additional configuration and features:
Note that this component has to be rendered as a child of LaunchDarkly
The flagKey
prop is the feature flag key you defined in LaunchDarkly.
The main callback function that renders your feature. In typical scenarios where your flag is a boolean, you can simply create your function to return the necessary JSX:
// Example FeatureFlag component
<FeatureFlag flagKey="example" renderFeatureCallback={this._renderFeature} />
// Callback function
_renderFeature () {
return (<div>New Feature Here!</div>);
}
When using a multivariate feature flag, the renderFeatureCallback
prop will pass the value of
the flag as an argument to your callback function:
// Example FeatureFlag component
<FeatureFlag flagKey="multivariate-example" renderFeatureCallback={this._renderFeature} />
// Callback function with feature flag value passed in
_renderFeature (featureFlagValue) {
if (featureFlagValue === "A") {
return (<div>Bucket "A" Feature!</div>);
}
return (<div>Default Bucket Feature Here!</div>);
}
Since the feature flags are requested from LaunchDarkly after DOM load, there may be some latency in the rendering. This render callback allows you to provide some sort of feedback to indicate loading, e.g., the typical spinning loader.
This callback is provided for cases where you want to render something by default, think of it when your feature flag is "off" or falsy.
SSR is opt-in and you need to specify the initial set of feature flag keys and values through
the bootstrap
property on clientOptions
:
// currentUser.featureFlags
// >> { "your-feature-flag": true }
const clientOptions = {
bootstrap: currentUser.featureFlags
};
What this gives you is that on SSR, we use the set of feature flags found on bootstrap
to render
your FeatureFlag
component. When your FeatureFlag
component is mounted, it will then initialize
the LaunchDarkly js-client and make the proper XHRs to LaunchDarkly to populate the available
feature flags within js-client's internal state. Thus taking precedence over the feature flags
present in bootstrap
.
In the event that you opt-in for SSR, you may not want to make any additional XHRs to LaunchDarkly
since you already have the feature flags provided from your server through bootstrap
, you can
disable this by supplying disableClient: true
:
const clientOptions = {
bootstrap: currentUser.featureFlags,
disableClient: true
};
If you need to temporarily override the variation reported by a feature flag for testing or demonstration purposes, you can do so using special query parameters in the request URL. This can be useful for seeing the possible effects of enabling a feature flag or to force a specific variation of a multivariate flag.
Do note that overriding a feature flag does not report it to LaunchDarkly nor does it persist. It's merely a mechanism for testing or demonstration purposes. One notable use-case is in integration and/or end-to-end testing.
You can enable a set of boolean feature flags with a comma-delimited list in the
features
query parameter:
// Overrides the `send-onboarding-email` boolean feature flag, setting it to `true`
http://localhost/users?features=send-onboarding-email
// Enables the `show-user-email`, `user-nicknames`, and `hide-inactive-users` feature flags
http://localhost/users/101?features=show-user-email,user-nicknames,hide-inactive-users
If you need to temporarily set a boolean feature flag to false
or override the
variation reported by a multivariate feature flag, you can use
features.{feature_flag}
query parameters:
// Disables the `verify-email` feature flag and sets the `email-frequency` variation to "weekly"
http://localhost/users?features.verify-email=false&features.email-frequency=weekly
The values "true" and "false" are converted into true
and false
boolean
values. If the query parameter value is omitted, then the feature flag will be
reported as enabled:
// Enables the `show-user-email` feature flag
http://localhost/users/101?features.show-user-email
// Overrides the `send-onboarding-email` boolean feature flag, setting it to `true`
http://localhost/users?features=send-onboarding-email
// Enables the `show-user-email`, `user-nicknames`, and `hide-inactive-users` feature flags
http://localhost/users/101?features=show-user-email,user-nicknames,hide-inactive-users
// Disables the `verify-email` feature flag and sets the `email-frequency` variation to "weekly"
http://localhost/users?features.verify-email=false&features.email-frequency=weekly
// Enables the `show-user-email` feature flag
http://localhost/users/101?features.show-user-email
If you need to change the configured user for the launch darkly client you can do that by calling identify
.
import { identify } from "react-launch-darkly";
identify(launchDarklyClientKey, launchDarklyUser, optionalUserHash);
See Launch Darkly's documentation for more information.