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

launch-darkly-react-native-client-factory

v0.0.17

Published

An ergonomic wrapper around the launchdarkly-react-native sdk, featuring *hooks*

Downloads

20

Readme

Launch Darkly React Native Client Factory

This is an ergonomic wrapper around the launchdarkly-react-native-client sdk package.

It makes using launch darkly feature flags in your React Native components this easy:

const MyComponent = () => {
  const myFF = useFeatureFlag('my-feature-flag')
  //...
}

The feature flags returned by this hook are strongly typed (if you're using typescript), and update and rerender your component dynamically.

installation

This package assumes that it will be used in a React Native application.

If you don't already have it, you will need the the launchdarkly-react-native-client sdk

npm i launchdarkly-react-native-client

and, of course

npm i launch-darkly-react-native-client-factory

usage

This factory will create a context provider, hooks and some escape hatch functions that you will need to re-export for use in your application.

Create a file called LaunchDarkly.ts and put this in it

// LaunchDarkly.ts
import { LaunchDarklyReactNativeFactory } from 'launch-darly-react-native-client-factory'

export const {
  LaunchDarklyProvider,
  useFeatureFlag,
  useAllFeatureFlags,
  getGlobalLdClient,
  getFeatureFlag,
} = LaunchDarklyReactNativeClientFactory({
  // **************************************************************
  // ! any feature flags that your app uses must be declared here !
  // ***************************************************************
  'example-boolean-feature-flag': { type: Boolean, defaultVal: false },
  'example-number-feature-flag': { type: Number, defaultVal: 123 },
  'example-string-feature-flag': { type: String, defaultVal: 'asdf' },
  'example-json-feature-flag': { type: Object, defaultVal: {} },
})

// Note: You may also define context and config asynchronously via hooks with their initial value as undefined.
// If you do this then the launch darkly client won't be initialized until context and config are.
// Your app will render with all feature flags set to their defaults until everything is ready to go.
export const context = {
  // learn how to create your launch darkly context and config here
  // https://docs.launchdarkly.com/sdk/client-side/react/react-native
}
export const config = {
  // learn how to create your launch darkly context and config here
  // https://docs.launchdarkly.com/sdk/client-side/react/react-native
}

You will need to add the provider to your app.tsx like this:

// App.tsx
import React from 'react';
import { LaunchDarklyProvider, context, config } from './LaunchDarkly';

export const app () => {
  return (
    <LaunchDarklyProvider context={context} config={config}>
      <TheRestOfYourAppGoesHere />
    </LaunchDarklyProvider>
  );
}

now, in your react native component, use it like this:

// ExampleComponent.tsx
import React from 'react'
import { useFeatureFlag } from './LaunchDarkly'
import { Text } from 'react-native'

export const ExampleComponent = () => {
  const darklyText = useFeatureFlag('example-string-feature-flag')
  const enableFancyFeature = useFeatureFlag('example-boolean-feature-flag')
  return (
    <>
      <Text>Example component</Text>
      <Text>{DarklyText}</Text>
      {enableFancyFeature && <Text>Fancy Feature Enabled!</Text>}
    </>
  )
}

This library has a few other features (such as overriding a flag's default value). You will find it (mostly) documented in the jsdocs comments in the codebase.