npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@lectra/ld-react-feature-flags

v1.3.0

Published

Integrate Launch Darkly in your React app in a breeze

Downloads

49

Readme

ld-react-feature-flags

Build Status NPM code 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.

import { FlagsProvider } from '@lectra/ld-react-feature-flags';

ReactDOM.render(
  <FlagsProvider user={user} clientkey="myClientKey" loadingComponent ={<div>please wait</div>}>
    <App />
  </FlagsProvider>,
  document.getElementById('root')
);

| Prop | Type | Required | Description | |------------------|-----------|----------|------------------------------| | user | Object | true | User information | | clientkey | String | true | Your LaunchDarkly secret key | | onFlagsChange | function | false | Handler for flag change | | loadingComponent | Component | false | Loading 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.

| Prop | Type | Required | Description | |----------------|-------------------|----------|-----------------------------------------------------------| | flag | String | true | The flag to check | | children | Element/Component | false | Return the component if the flag given by props is active | | renderOn | Function | false | Return the given component if the flag is active | | fallbackRender | Function | false | Return the given component if the flag is inactive |

with children props

import { Flags } from '@lectra/ld-react-feature-flags';

<Flags
  flag="beta-only"
>
  <h4>for beta users</h4>
</Flags>

with renderOn props

import { Flags } from '@lectra/ld-react-feature-flags';

<Flags
  flag="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';

<Flags
  flag="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';

<Flags
  flag="header-bg-color"
  renderOn={flag => {
    return (
      <h1 style={{ color: flag.headerBgColor }}>
        My awesome multivariant flag
      </h1>
    );
  }}
/>

WithFlags

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

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

| Arguments | Type | Required | Description | |--------------------------|-----------------|----------|-----------------------------------------------------------------------------| | flag | String | true | The flag to check | | ComponentToRenderIfTrue | React Component | true | The React component to render if the flag is true or is a multivariant flag | | ComponentToRenderIfFalse | React Component | false | The React component to render if the flag is false |

Component render based on flag value

import { WithFlags } from '@lectra/ld-react-feature-flags';

const HBeta = () => <h4>for beta users</h4>;
const HeaderFeatureFlipped = WithFlags("beta-only")(HBeta)

<HeaderFeatureFlipped></HeaderFeatureFlipped>

Component render toggled on flag value

import { WithFlags } from '@lectra/ld-react-feature-flags';

const HBeta = () => <h4>for beta users</h4>;
const HStandard = () => <h4>for standard users</h4>;
const HeaderFeatureFlipped = WithFlags("beta-only")(HBeta, HStandard)

<HeaderFeatureFlipped></HeaderFeatureFlipped>

Component render with multivariant flag

import { WithFlags } from '@lectra/ld-react-feature-flags';

const HeaderWithColor = ({headerBgColor}) => (
    <h1 style={{ color: headerBgColor }}>
        My awesome multivariant flag
    </h1>);
const HeaderFeatureFlippedWithColor = 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