GithubHelp home page GithubHelp logo

alexxnica / react-feature-toggles Goto Github PK

View Code? Open in Web Editor NEW

This project forked from paralleldrive/react-feature-toggles

0.0 0.0 0.0 174 KB

Feature Toggles for React Projects

License: MIT License

JavaScript 100.00%

react-feature-toggles's Introduction

react-feature-toggles

Build Status

Requirements

React Feature Toggles attempts to satisfy the following requirements:

  • Universal - server and client side
  • Conditionally execute code based on the presence or absence of a specific feature
  • Toggle features on with url parameters
  • Feature Dependency - if a feature depends on a another feature that is disabled, then neither of them should execute

Install

npm install --save @paralleldrive/react-feature-toggles

Setup

Setup withFeatures in the page HOC composition

import { withFeatures } from '@paralleldrive/react-feature-toggles';
import initialFeatures from '../config/initial-features';

const pageHOC = compose(
  withRedux,
  withFeatures({ initialFeatures }),
  hoc1,
  hoc2
);

Configure the component fallback:

import NotFound from '../components/not-found-component';
import { configureFeature } from '@paralleldrive/react-feature-toggles';

const requiresFeature = configureFeature(NotFound);

export default requiresFeature;

Build your component that requires a specific feature:

import compose from 'lodash/fp/compose';

import requiresFeature from '../hocs/requiresFeature';
import MyComponent from '../components/my-component';
import pageHOC from '../hocs/page';

const MyPage = compose(
  pageHOC, // withFeatures should be in there
  requiresFeature('feature-name'),
  // Optionally, anything special for this page should be here, e.g.,
  // requiresRole('admin')
)(MyComponent);

export default MyPage;

API

withFeatures()

Creates an array of enabled features, then sets the features array into React context and passes it onto the wrapped component via props.

// withFeatures() function signature.
const withFeatures = ({
  initialFeatures = [],
  windowLocationSearch = ""
} = {}) => (WrappedComponent: ReactComponent) => ReactComponent

initialFeatures

interface Feature {
  name: String,
  enabled: false,
  dependencies?: [...featureName: String]
}
const initialFeatures = [ ...Feature]

windowLocationSearch

Should be a string that is equivalent to the browser window.location.search; this is mostly used for testing purposes.

const windowLocationSearch = '?ft=comments'

configureFeature()

Conditionally renders components based on enabled features in the React context.

// configureFeatures() function signature
const configureFeature =
  (DefaultFallbackComponent: ReactComponent) =>
  (featureName: String) =>
  (FeatureComponent: ReactComponent, FallbackComponent = DefaultFallbackComponent) => ReactComponent

Utils

getEnabled

Returns all the names of enabled features.

getEnabled(features: [...Feature]) => featureNames: [...String]

getIsEnabled

Returns the enabled value of a single feature. If the feature does not exist it is considered disabled.

getIsEnabled(features: [...Feature], featureName: String, ) => enabled: Boolean

Enabling a feature from the url

NOTE: If you are using server rendering then overriding features from the url will cause React to throw a warning that the client-side HTML result is different from the server.

Add comma-separated names to the ft search param. ?ft=FEATURE_NAME,FEATURE_NAME

example

http://www.domain.com/?ft=help,comments

react-feature-toggles's People

Contributors

ericelliott avatar kennylavender avatar thoragio avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.