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

@superflag/client-react

v0.0.9

Published

![Coverage](static/coverage.svg)

Downloads

1

Readme

@superflag/client-react

Coverage

Superflag is an extensible feature flag library designed to support multiple tiers of feature flag infrastructure, from a simple constant to staged rollouts. It is designed to support multiple flag backends via an extensible plugin-style architecture that allows for easy upgrades to different flag systems with little to no maintenance overhead.

Installation

The base Superflag package includes multiple default flag source plugins and includes support for Typescript out of the box.

npm i @superflag/client-react

API Reference

Usage Overview

Configure Flag Sources (required)

The flag provider uses a basic React Context to pass down the fetched flag values to either the hooks or default components. In order for the other utilities in the package to function properly, one must configure the Flag Provider to wrap any code that depends on it (probably best at the root level of the project).

The FeatureFlagProvider helper component simplifies the process of setting up feature flags to a single component. Here's an example App with the flag provider configured with a constant value flag source:

// ...
import { FeatureFlagProvider } from '@superflag/client-react';
import { ConstantFlagSource } from '@superflag/client-react/build/defaultSources/constant';

const App = () => {
  // ...

  return (
    <IntlProvider>
      <FeatureFlagProvider
        flagSource={
          new ConstantFlagSource({
            useV2ui: false,
            useNewPaymentGateway: true,
          })
        }>
        <div>
          <Header />
          <Content />
          <Footer />
        </div>
      </FeatureFlagProvider>
    </IntlProvider>
  );
};

Extra flexibility

In the unlikely event that you need more flexibility, you can separate the provider and context declaration for even more extensibility via a combination of useFeatureFeatureFlagProvider and FeatureFlagProviderRaw. Here's the same example but with the calls split out:

// ...
import {
  FeatureFlagProviderRaw,
  useFeatureFlagProvider,
} from '@superflag/client-react';
import { ConstantFlagSource } from '@superflag/client-react/build/defaultSources/constant';

const App = () => {
  const flagContext = useFeatureFlagProvider({
    flagSource: new ConstantFlagSource({
      useV2ui: false,
      useNewPaymentGateway: true,
    }),
  });

  // ...

  return (
    <IntlProvider>
      <FeatureFlagProviderRaw {...flagContext}>
        <div>
          <Header />
          <Content />
          <Footer />
        </div>
      </FeatureFlagProviderRaw>
    </IntlProvider>
  );
};

Consuming flags

There are multiple hooks and convenience components available. Both can be used together if desired, the convenience components just reduce the amount of identical code by handling common use cases (hiding/showing a component based on a flag).

Hooks:

  • useFeatureFlagContext() - returns the entire flag context, including loading, the flags, and the identify function
  • useFeatureFlags() - returns all feature flags in key-value form
  • useFeatureFlag(flagKey) - returns a single flag value

Components:

  • <FeatureFlagGate flagKey={...}> - Hides its children if the flag is false, shows the children if the flag is true
  • <FeatureFlagSwitch whenTrue={...} whenFalse={...}> - shows the passed value of whenFalse if the flag is false, shows the passed value of whenTrue if the flag is true. Both props are optional

Here's an example where everything is used:

import {
  useFeatureFlagContext,
  useFeatureFlags,
  useFeatureFlag,
  FeatureFlagGate,
  FeatureFlagSwitch,
} from '@superflag/client-react';

const ShoppingCart = () => {
  // option 1
  const { loading, flags } = useFeatureFlagContext();
  // option 2
  const { useV2ui } = useFeatureFlags();
  // option 3
  const useNewPaymentGateway = useFeatureFlag('useNewPaymentGateway');

  // apollo GQL mutation function
  const [checkout] = useMutation(endpoints.checkout);

  return (
    <div>
      {loading && <LoadingSpinner />}
      {/* option 1 */}
      <h1>{useV2ui ? 'Cart' : 'New and Improved Cart'}</h1>
      {/* option 2 */}
      <FeatureFlagGate flagKey="useV2ui">
        <input name="v2-input" />
      </FeatureFlagGate>
      {/* option 3*/}
      <FeatureFlagSwitch
        flagKey="useV2ui"
        whenTrue={<textarea name="v2-textarea" />}
        whenFalse={<select name="v1-select" />}
      />

      <button
        onClick={() => {
          checkout({ variables: { price: useNewPaymentGateway ? 1.5 : 2.5 } });
        }}>
        Checkout
      </button>
    </div>
  );
};

Testing

Superflag supports mocking flags for testing purposes in one of two ways:

  • For unit tests, MockFeatureFlagProvider can be used to mock the feature flag values
  • For integration/e2e testing (live), one can swap out the flagSource when in different environments. For example, one could switch a LaunchDarkly flag source out for a constant flag source in testing/development environments

Bugs & Contributions

Please report any bugs to the Github issue tracker for this project. Any bug reports or PR submissions are appreciated!