项目作者: yusinto

项目描述 :
Launch Darkly React integration with hooks
高级语言: JavaScript
项目地址: git://github.com/yusinto/ld-react.git
创建时间: 2018-05-29T23:07:12Z
项目社区:https://github.com/yusinto/ld-react

开源协议:MIT License

下载


ld-react

npm version npm downloads npm npm

This package has been superseded by the official LaunchDarkly React SDK. Please use that instead.

The quickest and easiest way to integrate launch darkly with react :tada:

Why this package?

  • Easy and fast to use. Two steps to get feature flags into your react app.
  • Supports subscription out of the box. You get live changes on the client as you toggle features.
  • You automatically get camelCased keys as opposed to the default kebab-cased.
  • No need for redux! This package uses the new context api which is available from react ^16.3.0.

Dependency

This needs react ^16.4.0! It won’t work otherwise.

Installation

yarn add ld-react

Quickstart

  1. Wrap your root app withFlagProvider:

    1. import {withFlagProvider} from 'ld-react';
    2. const App = () =>
    3. <div>
    4. <Home ></Home>
    5. </div>;
    6. export default withFlagProvider(App, {clientSideId: 'your-client-side-id'});
  2. Wrap your component withFlags to get them via props:

    1. import {withFlags} from 'ld-react';
    2. const Home = props => {
    3. // flags are available via props.flags
    4. return props.flags.devTestFlag ? <div>Flag on</div> : <div>Flag off</div>;
    5. };
    6. export default withFlags(Home);

That’s it!

API

withFlagProvider(Component, {clientSideId, user, options})

This is a hoc which accepts a component and a config object with the above properties.
Component and clientSideId are mandatory.

For example:

  1. import {withFlagProvider} from 'ld-react';
  2. const App = () =>
  3. <div>
  4. <Home ></Home>
  5. </div>;
  6. export default withFlagProvider(App, {clientSideId: 'your-client-side-id'});

The user property is optional. You can initialise the sdk with a custom user by specifying one. This must be an object containing
at least a “key” property. If you don’t specify a user object, ld-react will create a default one that looks like this:

  1. const defaultUser = {
  2. key: uuid.v4(), // random guid
  3. ip: ip.address(),
  4. custom: {
  5. browser: userAgentParser.getResult().browser.name,
  6. device
  7. }
  8. };

For more info on the user object, see here.

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

  1. withFlagProvider(Component, {
  2. clientSideId,
  3. options: {
  4. bootstrap: 'localStorage',
  5. },
  6. });

withFlags(Component)

This is a hoc which passes all your flags to the specified component via props. Your flags will be available
as camelCased properties under this.props.flags. For example:

  1. import {withFlags} from 'ld-react';
  2. class Home extends Component {
  3. render() {
  4. return (
  5. <div>
  6. {
  7. this.props.flags.devTestFlag ? // Look ma, feature flag!
  8. <div>Flag on</div>
  9. :
  10. <div>Flag off</div>
  11. }
  12. </div>
  13. );
  14. }
  15. }
  16. export default withFlags(Home);

ldClient

Internally the ld-react initialises the ldclient-js sdk and stores a reference to the resultant ldClient object in memory.
You can use this object to access the official sdk methods directly.
For example, you can do things like:

  1. import {ldClient} from 'ld-react';
  2. class Home extends Component {
  3. // track goals
  4. onAddToCard = () => ldClient.track('add to cart');
  5. // change user context
  6. onLoginSuccessful = () => ldClient.identify({key: 'someUserId'});
  7. // ... other implementation
  8. }

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

Example

Check the example for a fully working spa with
react and react-router. Remember to enter your client side sdk in the client root app file
and create a test flag called dev-test-flag before running the example!