4.0.1 • Published 15 days ago

react-native-flagsmith v4.0.1

Weekly downloads
178
License
BSD-3-Clause
Repository
github
Last release
15 days ago

Flagsmith Client

npm version npm.io

The SDK clients for web and React Native for https://flagsmith.com/. Flagsmith allows you to manage feature flags and remote config across multiple projects, environments and organisations.

Getting Started

For full documentation visit https://docs.flagsmith.com/clients/javascript/

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See running in production for notes on how to deploy the project on a live system.

Usage

npm i react-native-flagsmith --save

Retrieving feature flags for your project

import flagsmith from "react-native-flagsmith"; //Add this line if you're using flagsmith via npm

flagsmith.identify("flagsmith_sample_user");

flagsmith.init({
	environmentID:"<YOUR_ENVIRONMENT_KEY>",
    cacheFlags: true,
	onChange: (oldFlags,params)=>{ //Occurs whenever flags are changed
	
		const {isFromServer} = params; //determines if the update came from the server or local cached storage
		
		//Check for a feature
		if (flagsmith.hasFeature("myCoolFeature")){
			myCoolFeature();
		}
				
		//Or, use the value of a feature
		const bannerSize = flagsmith.getValue("bannerSize");
		
		//Check whether value has changed
		const bannerSizeOld = oldFlags["bannerSize"] && oldFlags["bannerSize"].value;
		if (bannerSize !== bannerSizeOld) {
		
		}

	}
});

Initialisation options

See all available types here.

PropertyDescriptionRequiredDefault Value
environmentID: stringDefines which project environment you wish to get flags for. example ACME Project - Staging.YESnull
onChange?: (previousFlags:IFlags, params:IRetrieveInfo)=> voidYour callback function for when the flags are retrieved (flags,{isFromServer:true/false})=>{...}YESnull
onError?: (res:{message:string}) => voidCallback function on failure to retrieve flags. (error)=>{...}null
asyncStorage?:anyNeeded for cacheFlags option, used to tell the library what implementation of AsyncStorage your app uses, i.e. ReactNative.AsyncStorage vs @react-native-community/async-storage, for web this defaults to an internal implementation.null
cacheFlags?: booleanAny time flags are retrieved they will be cached, flags and identities will then be retrieved from local storage before hitting the API ` | | null
enableAnalytics?: booleanEnable sending flag analytics for getValue and hasFeature evaluations.false
enableLogs?: booleanEnables logging for key Flagsmith eventsnull
defaultFlags?: IFlagsAllows you define default features, these will all be overridden on first retrieval of features.null
preventFetch?: booleanIf you want to disable fetching flags and call getFlags later.false
state?: IStateSet a predefined state, useful for SSR / isomorphic applications.false
api?: stringUse this property to define where you're getting feature flags from, e.g. if you're self hosting.https://api.flagsmith.com/api/v1/

Available Functions

PropertyDescription
init(initialisationOptions)=> Promise<void>Initialise the sdk against a particular environment
hasFeature(key:string)=> booleanGet the value of a particular feature e.g. flagsmith.hasFeature("powerUserFeature") // true
getValue(key:string)=> string|number|booleanGet the value of a particular feature e.g. flagsmith.getValue("font_size") // 10
getTrait(key:string)=> string|number|booleanOnce used with an identified user you can get the value of any trait that is set for them e.g. flagsmith.getTrait("accepted_cookie_policy")
getState()=>IStateRetrieves the current state of flagsmith, useful in NextJS / isomorphic applications. flagsmith.getState()
setTrait(key:string, value:string|number|boolean)=> Promise<IFlags>Once used with an identified user you can set the value of any trait relevant to them e.g. flagsmith.setTrait("accepted_cookie_policy", true)
setTraits(values:Record<string, string|number|boolean>)=> Promise<IFlags>Set multiple traits e.g. flagsmith.setTraits({foo:"bar",numericProp:1,boolProp:true}). Setting a value of null for a trait will remove that trait.
incrementTrait(key:string, value:number)=> Promise<IFlags>You can also increment/decrement a particular trait them e.g. flagsmith.incrementTrait("click_count", 1)
startListening(ticks=1000:number)=>voidPoll the api for changes every x milliseconds
stopListening()=>voidStop polling the api
getFlags()=> Promise<IFlags>Trigger a manual fetch of the environment features, if a user is identified it will fetch their features. Resolves a promise when the flags are updated.
identify(userId:string, traits?:Record<string, string|number|boolean>)=> Promise<IFlags>Identify as a user, optionally with traits e.g. {foo:"bar",numericProp:1,boolProp:true}. This will create a user for your environment in the dashboard if they don't exist, it will also trigger a call to getFlags(), resolves a promise when the flags are updated.
logout()=>Promise<IFlags>Stop identifying as a user, this will trigger a call to getFlags()

Notes on initialisation

identify, setTrait and setTraits all trigger calls to getFlags, which in turn hits the get flags endpoint. This is due to identities and traits affecting flags that are returned.

However, you can avoid these extra calls to get flags if you call these functions before flagsmith.init.

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

Getting Help

If you encounter a bug or feature request we would like to hear about it. Before you submit an issue please search existing issues in order to prevent duplicates.

Get in touch

If you have any questions about our projects you can email projects@solidstategroup.com.

4.0.1

15 days ago

4.0.0

1 month ago

3.24.0

1 month ago

3.23.1

2 months ago

3.23.2

2 months ago

3.23.0

2 months ago

3.23.0-beta.4

3 months ago

3.23.0-beta.5

3 months ago

3.23.0-beta.2

3 months ago

3.23.0-beta.3

3 months ago

3.22.1

3 months ago

3.23.1-beta.1

3 months ago

3.23.0-beta.1

3 months ago

3.22.0

4 months ago

3.22.0-beta.1

5 months ago

3.20.0

7 months ago

3.21.0

6 months ago

3.19.0-beta-1

12 months ago

3.19.0-beta-2

12 months ago

3.18.5-beta5

11 months ago

3.18.5-beta6

11 months ago

3.18.5-beta1

11 months ago

3.18.5-beta3

11 months ago

3.18.5-beta2

11 months ago

3.19.0

11 months ago

3.19.1

10 months ago

3.17.0-beta-9

1 year ago

3.17.0-beta-8

1 year ago

3.17.0-beta-7

1 year ago

3.17.0-beta-6

1 year ago

3.17.0-beta-5

1 year ago

3.17.0-beta-4

1 year ago

3.17.0-beta-3

1 year ago

3.17.0-beta-2

1 year ago

3.17.0-beta-1

1 year ago

3.18.1

1 year ago

3.18.0

1 year ago

3.18.3

1 year ago

3.18.2

1 year ago

3.16.0-beta-1

1 year ago

3.15.2

1 year ago

3.16.0

1 year ago

3.17.0-beta-10

1 year ago

3.16.0-beta-2

1 year ago

3.16.0-beta-3

1 year ago

3.16.0-beta-4

1 year ago

3.16.0-beta-5

1 year ago

3.13.1

1 year ago

3.12.0

2 years ago

3.14.1

1 year ago

3.10.5

2 years ago

3.14.0

1 year ago

3.10.4

2 years ago

3.14.3

1 year ago

3.14.2

1 year ago

3.10.6

2 years ago

3.13.0

2 years ago

3.15.0

1 year ago

3.15.1

1 year ago

3.10.3

2 years ago

3.11.0

2 years ago

3.9.2

2 years ago

3.9.1

2 years ago

3.9.0

2 years ago

3.10.1

2 years ago

3.10.0

2 years ago

3.10.2

2 years ago

3.9.3-beta.1

2 years ago

3.9.1-beta.1

2 years ago

3.9.1-beta.2

2 years ago

3.7.2

2 years ago

3.4.0

2 years ago

3.0.4

2 years ago

3.0.3

2 years ago

3.2.0

2 years ago

3.0.2

2 years ago

3.0.1

2 years ago

3.8.0

2 years ago

3.6.0

2 years ago

3.0.6

2 years ago

3.4.1

2 years ago

3.0.5

2 years ago

3.0.0-beta.1

2 years ago

3.6.0-beta.1

2 years ago

3.6.0-beta.2

2 years ago

3.6.0-beta.3

2 years ago

3.6.0-beta.4

2 years ago

2.0.10

2 years ago

3.7.0-beta.4

2 years ago

3.8.1

2 years ago

3.7.0-beta.3

2 years ago

3.7.0-beta.2

2 years ago

3.7.0-beta.1

2 years ago

3.3.1

2 years ago

2.1.0-beta.1

2 years ago

3.3.0

2 years ago

3.1.2

2 years ago

3.1.1

2 years ago

3.1.0

2 years ago

3.7.1

2 years ago

3.7.0

2 years ago

3.5.0

2 years ago

3.3.2

2 years ago

3.8.1-beta.5

2 years ago

3.8.0-beta.4

2 years ago

3.8.1-beta.1

2 years ago

3.8.0-beta.2

2 years ago

3.8.1-beta.2

2 years ago

3.8.0-beta.3

2 years ago

3.8.1-beta.3

2 years ago

3.5.0-beta.1

2 years ago

3.8.1-beta.4

2 years ago

3.8.0-beta.1

2 years ago

3.5.0-beta.2

2 years ago

2.0.3

2 years ago

2.0.2

2 years ago

2.0.5

2 years ago

2.0.4

2 years ago

2.0.7

2 years ago

2.0.6

2 years ago

2.0.9

2 years ago

2.0.8

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.7.3

2 years ago

1.7.2

2 years ago

1.7.1

2 years ago

1.7.4

2 years ago

1.7.0

3 years ago

1.6.9

3 years ago

1.6.8

3 years ago

1.6.10

3 years ago

1.6.7

3 years ago

1.6.6

3 years ago

1.6.5

3 years ago

1.6.2

3 years ago

1.6.1

3 years ago

1.6.0

3 years ago

1.5.1

3 years ago

1.5.0

3 years ago

1.4.1

3 years ago

1.4.0

3 years ago

1.3.0

3 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.2.0

3 years ago

1.1.1

3 years ago

1.1.2

3 years ago

1.1.0

3 years ago

1.0.0

4 years ago