React Flagship SDK provides a <FlagshipProvider />
, which makes Flagship features available to the rest of your app.
Flagship features are accessible using Flagship hooks, have a look to the documentation for details.
-
Node.js: version 6.0.0 or later...
-
Npm: version 5.2.0 or later...
-
React: version 16.8.0 or later... (This SDK supports only hooks for now)
-
- Typescript supported ✅
-
- Small dev demo app but more to come soon! 👨💻
- Install the node module:
npm install @flagship.io/react-sdk
- Import the Flagship React provider at the root level of your app like
App.js
file :
import React from "react";
import { FlagshipProvider } from "@flagship.io/react-sdk";
const App = () => (
<>
<FlagshipProvider>
{/* [...] */}
</FlagshipProvider>
</>
);
- Initialize the provider with at least required props such as
envId
,visitorData
:
import React from "react";
import { FlagshipProvider } from "@flagship.io/react-sdk";
const App = () => (
<>
<FlagshipProvider
envId="bn1ab7m56qolupi5sa0g"
visitorData={{
id: "test-visitor-id",
context: {},
}}
config={{
fetchNow: true,
enableConsoleLogs: true,
}}
>
{/* [...] */}
</FlagshipProvider>
</>
);
- Use a Flagship hook in a component. In most case, you'll want to have modifications in cache :
import React from 'react';
import { useFsModifications } from "@flagship.io/react-sdk";
export const MyReactComponent = () => {
const fsModifications = useFsModifications([
{
key: "backgroundColor",
defaultValue: "green",
activate: false,
},
]);
return (
<div
style={{
height: "200px",
width: "200px",
backgroundColor: fsModifications.backgroundColor,
}}
>
{"I'm a square with color=" + fsModifications.backgroundColor}
</div>
)
}
This is all available props which you can use inside the FlagshipProvider
react component:
Props | Type | Default | Description | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
envId | string | *required* | Your Flagship environment id. | ||||||||||||
visitorData | object | *required* | This is the data to identify the current visitor using your app. The visitorData object takes the following attributes:
|
||||||||||||
config | object | {} | This is the settings of the SDK. It takes an object which is the same shape as the JS SDK settings. Go have a look, there are many options. 🙂 | ||||||||||||
onInitStart | function():void | null | Callback function called when the SDK starts initialization. | ||||||||||||
onInitDone | function():void | null | Callback function called when the SDK ends initialization. | ||||||||||||
onUpdate | function():void | null | Callback function called when the SDK is updated. For example, after a synchronize is triggered or visitor context has changed. | ||||||||||||
onSavingModificationsInCache | function(obj):void | null | Callback function called when the SDK is saving modifications in cache.
It has an argument which has the following shape:
|
||||||||||||
defaultModifications | object | null | This is an object which has the shape of Flagship modifications as it is return from the Flagship API. Can be useful when you already manually fetched the data before or you have your own cache. Providing this props avoid the SDK to have an empty cache during first initialization. The default modifications provided will be override once the SDK finish to fetch Flagship API with an initialization or a synchronization. |
||||||||||||
loadingComponent | React.ReactNode | undefined | This is component which will be render when Flagship is loading on first initialization only. By default, the value is undefined which means it will display your app and it might display default modifications value for a very short moment. |
Here the list of current available hooks:
Most used hook from the Flagship React SDK. Through this hook, you can access to modifications of your current visitor and have an access to the SDK status.
returns an object (Typescript: UseFlagshipOutput)
Argument | Type | Default | description |
---|---|---|---|
params | object (TS:UseFlagshipOutput) | *required* | Contains further required nodes. See the shape of this object just below. |
Argument | Type | description | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
modifications | Object | Node param to specify flagship modifications:
|
||||||||||||||
status | Object | Gives you some informations about SDK current sate:
|
This will give you the modification saved in the SDK cache.
NOTE: If the SDK cache is empty, you can expect that it will return nothing.
returns Flagship modifications
Argument | Type | Default | description | ||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
modificationsRequested | Array(object) | *required* | List of all modifications you're looking for. Each element of the array follow this object structure:
|
||||||||
activateAllModifications | Boolean | false | If set to true, all modifications will be activated. If set to false, none will be activated.
Be aware that if this argument is set, the attribute activate set in each element of array modificationsRequested will be ignored. |
Demo:
- coming soon
return
void
Argument | Type | Default | description |
---|---|---|---|
modificationKeys | Array(string) | *required* | An array of modification key. For each key, a http will be done to trigger the activate of corresponding modification. |
applyEffectScope | Array(string) | [] | This argument has same behavior as React.useEffect (2nd argument) hook. It will listen values inside array and trigger a synchronize if one them has changed. By default it is trigger once, during React component where it's used, did mount. |
Demo:
- coming soon
Refresh modifications in cache by making a http request to the Flagship API.
return
void
Argument | Type | Default | description |
---|---|---|---|
applyEffectScope | Array(string) | [] | This argument has same behavior as React.useEffect (2nd argument) hook. It will listen values inside array and trigger a synchronize if one them has changed. By default it is trigger once, during React component where it's used, did mount. |
activateAllModifications | Boolean | false | If set to true, all modifications will be activated. If set to false (default behavior), none will be activated. |
Demo:
- coming soon
Take a look to the Contributors Guide.
Have a look here.
Flagship uses license under the Apache version 2.0.