Socket
Book a DemoInstallSign in
Socket

flag

Package Overview
Dependencies
Maintainers
1
Versions
34
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

flag

Feature flagging made easy for React and Redux

1.0.3
Source
npmnpm
Version published
Maintainers
1
Created
Source

Flag

Feature flagging made easy for React and Redux

yarn add flag

Motivation

Feature flagging is necessary for large client-side applications. They improve development speed and allow teams to test new features before they are stable. In order to WANT to use feature flags in an application, they should be VERY easy to add and remove. That means minimal boiler plate and no need to pass boolean props down through component hierarchy. Such a thing could be done with global variables; however, they live outside of the React/Redux lifecycle, making them more difficult to control. Instead, this library injects and then accesses feature flags directly from the application Redux store without getting in your way.

Getting Started

Wrapping any part of the application in a feature flag is easy. First, we declare a flags top-level key as part of our reducer.

import { createStore, combineReducers } from 'redux';
import { createFlagsReducer } from 'flag';

const flags = {
  // properties can be nested objects
  features: {
    // they can be boolean
    useMyCoolNewThing: true,
  },
  config: {
    // they can be strings
    apiUrl: 'www.example.com/api',
  },
  // they can be numbers
  cool: 1,
  dude: 5,
  // they can be computed
  coolAndDude: flags => flags.cool + flags.dude,
  // they can be computed from other computed properties.
  largeCoolAndDude: flags => flags.coolAndDude > 10
};

const reducer = combineReducer({
  flags: createFlagsReducer(flags)
});

const store = createStore(reducer);

Now that I've created the store, I need only use the Flag component anywhere in my app hierarchy where I can specify which key to check and what to do in both the truthy and falsy cases.

import { Provider } from 'react-redux';
import { Flag } from 'flag';

const instance = (
  <Provider store={store}>
    <div>
      This is my application.
      <Flag
        name="features.useMyCoolNewThing"
        render={() =>
          <div>Rendered on truthy</div>
        }
        fallbackRender={() =>
          <div>Rendered on falsy</div>
        }
      />
    </div>
  </Provider>
)

API

Flag

The main React component.

PropTypeRequiredDescription
namestringtrueThe name of the feature to check
render(val: any) => ReactElementfalseThe render function if the flag is truthy
fallbackRender(val: any) => ReactElementfalseThe render function if the flag is falsy
<Flag
  name="flagA"
  render={(valueOfFlagA) => <TruthyFeature />}
  fallbackRender={(valueOfFlagA) => <FalsyFeature />}
/>

setFlagsAction

A dispatchable action that sets flags.

store.dispatch(
  setFlagsAction({
    flagA: false
  })
);

createFlagsReducer

Creates the reducer for your redux store. Accepts any plain object as an argument.

const myDefaultFlags = {
  features: {
    useMyCoolNewThing: true,
    useMyOtherThing: false,
    proAccount: ({features}) => features.useMyCoolNewThing && features.useMyOtherThing
  },
  config: {
    apiUrl: 'www.example.com/api',
  },
}

const reducer = combineReducers({
  flags: createFlagsReducer(myDefaultFlags)
})

License

MIT

FAQs

Package last updated on 29 May 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.