Skip to content

Repository files navigation

ld-react-feature-flags

Build StatusNPMcode style: prettier

Integrate Launch Darkly in your React app in a breeze

Install

This project requires React 16.3.0 or later.

To use ld-react-feature-flags with your React app, install it as a dependency:

npm install @lectra/ld-react-feature-flags

Getting Started

FlagsProvider

Wrap your root component with FlagsProvider to make LaunchDarkly client instance accessible to all children components thanks to React context.

// React 17 and lowerimportReactDOMfrom'react-dom';import{FlagsProvider}from'@lectra/ld-react-feature-flags';ReactDOM.render(<FlagsProvideruser={user}clientkey="myClientKey"loadingComponent={<div>please wait</div>}><App/></FlagsProvider>,document.getElementById('root'));
// React 18 and higherimport{createRoot}from'react-dom/client';import{FlagsProvider}from'@lectra/ld-react-feature-flags';constroot=createRoot(document.getElementById('root'));root.render(<FlagsProvideruser={user}clientkey="myClientKey"loadingComponent={<div>please wait</div>}><App/></FlagsProvider>);
PropTypeRequiredDescription
userObjecttrueUser information
clientkeyStringtrueYour LaunchDarkly secret key
onFlagsChangefunctionfalseHandler for flag change
loadingComponentComponentfalseLoading component / string

Flags

All Flags components get the ldClient instance thanks to the FlagsProvider component.

To render a node or a component based on your flags, you must pass a flag props.

The Flags component will ask to LaunchDarkly if the given flag is active or not, depending on you LaunchDarkly settings.

You have the control on what will be rendered:

  • If the flag is active, you can wrapped the desired component to render as children to a Flag component or use a renderOn props.

  • If the flag isn't active, nothing will be rendered unless you pass a component as fallback by the fallbackRender props.

PropTypeRequiredDescription
flagStringtrueThe flag to check
childrenElement/ComponentfalseReturn the component if the flag given by props is active
renderOnFunctionfalseReturn the given component if the flag is active
fallbackRenderFunctionfalseReturn the given component if the flag is inactive

with children props

import{Flags}from'@lectra/ld-react-feature-flags';<Flagsflag="beta-only"><h4>for beta users</h4></Flags>

with renderOn props

import{Flags}from'@lectra/ld-react-feature-flags';<Flagsflag="beta-only"renderOn={flag=><h4>for beta users</h4>}/>

with renderOn props and fallbackRender props as fallback

import{Flags}from'@lectra/ld-react-feature-flags';<Flagsflag="beta-only"renderOn={flag=><h4>for beta users</h4>}fallbackRender={flag=>(<h4>for regular users</h4>)}/>

with multivariant flag

The flag given by props is a multivariant flag. See the LaunchDarkly doc for more details.

If the flag is active, LD won't return a boolean value but instead a custom value. In our case a string that represents a color. We can use it directly to style our h1 title.

import{Flags}from'@lectra/ld-react-feature-flags';<Flagsflag="header-bg-color"renderOn={flag=>{return(<h1style={{color: flag.headerBgColor}}>
My awesome multivariant flag
</h1>);}}/>

WithFlags

Same as Flags components but in a Higher Order Component way.

WithFlags([flag])([ComponentToRenderIfTrue][ComponentToRenderIfFalse])

ArgumentsTypeRequiredDescription
flagStringtrueThe flag to check
ComponentToRenderIfTrueReact ComponenttrueThe React component to render if the flag is true or is a multivariant flag
ComponentToRenderIfFalseReact ComponentfalseThe React component to render if the flag is false

Component render based on flag value

import{WithFlags}from'@lectra/ld-react-feature-flags';constHBeta=()=><h4>for beta users</h4>;constHeaderFeatureFlipped=WithFlags("beta-only")(HBeta)<HeaderFeatureFlipped></HeaderFeatureFlipped>

Component render toggled on flag value

import{WithFlags}from'@lectra/ld-react-feature-flags';constHBeta=()=><h4>for beta users</h4>;constHStandard=()=><h4>for standard users</h4>;constHeaderFeatureFlipped=WithFlags("beta-only")(HBeta,HStandard)<HeaderFeatureFlipped></HeaderFeatureFlipped>

Component render with multivariant flag

import{WithFlags}from'@lectra/ld-react-feature-flags';constHeaderWithColor=({headerBgColor})=>(<h1style={{color: headerBgColor}}>
My awesome multivariant flag
</h1>);constHeaderFeatureFlippedWithColor=WithFlags("header-bg-color")(HeaderWithColor)<HeaderFeatureFlippedWithColor></HeaderFeatureFlippedWithColor>

Example

This project contains some examples that you could run.

git clone https://github.com/lectra-tech/ld-react-feature-flags.git
cd ld-react-feature-flags/example
npm install
npm start

License

MIT

About

Integrate Launch Darkly in your React app in a breeze

Topics

Resources

Code of conduct

Contributing

Stars

10 stars

Watchers

5 watching

Forks

Releases

Packages

Used by

Contributors

Languages