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

@dexterns88/ld-redux

v4.0.6

Published

A library to integrate launch darkly with react redux

Downloads

3

Readme

@dexterns88 fork from @svey/ld-redux

NOTE: add support for React 16

A library to integrate launch darkly with react redux :clap:

Huge shout-out to yusinto. He created the base of this package and if he resumes maintaining I would happily integrate my updates into yusinto/ld-redux. Unfortunately, it looks like yusinto stopped maintaining yusinto/ld-redux and that's probably why you're here...

Launch Darkly is a great tool for feature flagging and a/b testing. It has a fully capable client-side javascript sdk, so why this package?

This repo picks up where yusinto left off and migrates the launchdarkly client-side javascript sdk from ^2.13.0 to ^3.1.3.

NPM

npm i @svey/ld-redux

Upgrade from yusinto/ld-redux

Update your user to a user context see migration documentation (if no context is passed a user context will be generated)

Update ldRedux.init to:

ldRedux.init({
        clientSideId,
        dispatch,
        context // previously this was user see migration documentation above
        ...
      });

Initializing ld-redux

  1. In your client bootstrap, initialise the launch darkly client by invoking the init method:
import ldRedux from '@svey/ld-redux';
import flags from '../flags';
import reduxState from '../store/state';

const store = createStore(reduxState);

const context = {
  kind: 'user',
  key: 'user-key-123abc',
  firstName: 'Sandy',
  lastName: 'Smith',
  email: '[email protected]',
  groups: ['Google', 'Microsoft']
}; // https://docs.launchdarkly.com/sdk/features/user-context-config 

ldRedux.init({
  clientSideId: '59b2b2596d1a250b1c78baa4',
  dispatch: store.dispatch,
  flags,
  context,
});
  1. Include ldReducer as one of the reducers in your app:

    import { combineReducers } from 'redux';
    import ldRedux from 'ld-redux';
    import reducers from '<your-project>/reducers';
    
    export default combineReducers({
      ...reducers,
      LD: ldRedux.reducer(), // Note: the LD key can be anything you want
    });
  2. Use the flag:

    import React, {Component} from 'react';
    import {connect} from 'react-redux';
    
    const mapStateToProps = (state) => {
      const {featureFlagKey} = state.LD; // Note: the key LD must be the same as step 2.
    
      return {
        featureFlagKey,
      };
    };
    
    @connect(mapStateToProps)
    export default class Home extends Component {
      render() {
        return (
          <div>
            {
              /* look ma, feature flag! */
              this.props.featureFlagKey ?
                <div>
                  <p>Welcome to feature toggling!</p>
                </div>
                :
                'nothing'
            }
          </div>
        );
      }
    }

API

init({clientSideId, dispatch, flags, useCamelCaseFlagKeys, context, subscribe, options})

The init method accepts an object with the above properties. clientSideId, dispatch are mandatory.

The flags property is optional. This is an object containing all the flags you want to use and subscribe to in your app. If you don't specify this, ld-redux will subscribe to all flags in your ld environment.

// standard redux createStore
const store = createStore();
const flags = { 'feature-flag-key': false }; // only subscribe to  this one flag

// do this once
ldRedux.init({
  clientSideId: 'your-client-side-id',
  dispatch: store.dispatch,
  flags,
});

The subscribe property is optional. This defaults to true which means by default you'll get automatic live updates of flag changes from the server. You can turn this off and manually subscribe to flag changes through the ldClient object if for some reason you don't want to get live updates.

The context property is optional. You can initialise the sdk with a custom context by specifying one. If you don't specify a context object, ldRedux will create a default user context that looks like this:

const context = {
  kind: 'user',
  key: 'user-key-123abc',
  firstName: 'Sandy',
  lastName: 'Smith',
  email: '[email protected]',
  groups: ['Google', 'Microsoft']
};

For more info on context object, see here.

The useCamelCaseFlagKeys property is optional. This defaults to true which means by default the flags that are stored in redux will be camel cased. If this property is false, no transformation on the flag name will be done.

The options property is optional. It can be used to pass in extra options such as Bootstrapping. For example:

ldRedux.init({
    clientSideId,
    dispatch,
    flags,
    options: {
      bootstrap: 'localStorage',
    }
});

reducer()

This is ld-redux's reducer. You must include this reducer in your app as per step 2 above with any key of your choice. You then use this key to retrieve your flags from redux's state.

window.ldClient

Internally the ldRedux.init method above initialises the js sdk and stores the resultant ldClient object in window.ldClient. You can use this object to access the official sdk methods directly. For example, you can do things like:

// track goals
window.ldClient.track('add to cart');

// change user context
window.ldClient.identify({key: 'someUserId'});

For more info on changing user context, see the official documentation.

isLDReady

You no longer need to deal with isLDReady. However if you need to, it is still available in the store. You can access it via the LD state like so:

const mapStateToProps = (state) => {
  const {isLDReady} = state.LD; // Note: the key LD must be the same as step 2.

  return {
    isLDReady,
  };
};

This is useful to solve "flickering" issues above the fold on your front page caused by a flag transitioning from a default false value to true.

Example

Check the example by yusinto for a fully working spa with react, redux and react-router. Remember to enter your client side sdk in the client bootstrap file before running the example!

Please feel free to open issues and PRs!