React
Guide
Setup
Add the client to your dependencies:
Add the provider to your app:
For server-side-rendering you can pass the list of flags directly to the client so the user does not have to re-fetch this list via the API.
Read the Node.js client documentation for more client specific information like polling, caching, and using the proxy.
Updating the context
You can now change the context anywhere in the app using the useTggl
hook:
updateContext
only updates the keys you specify, it merges the context you pass as argument into the existing context. Alternatively you can use setContext
to override the context completely.
Checking flag results
Use the useFlag
hook to get the state of a flag:
Typing
Using the Tggl CLI you can run an introspection query to generate the TypeScript types for your flags and context.
Replace <SERVER_API_KEY>
with your server API key or use the TGGL_API_KEY
environment variable and omit the -k
option. You should run this command everytime you need to update the typing. Your IDE will now autocomplete and type-check the context properties and all flag names and values.
Tracking flags evaluation events
Tggl works nicely with your already existing analytics tool. You can track all flag evaluation events in one place and forward it to your analytics to compute conversion rates for A/B testing.
Amplitude
If you are using Amplitude, tracking is automatic, you don't need to do anything. Just make sure the Amplitude SDK is available on window.amplitude
. [Tggl] Flag evaluated
events will be sent to Amplitude with slug and value.
If you are importing the Amplitude SDK via a <script />
tag, you do not need to do anything. If you are using npm, you need to expose the amplitude
object to the window object like so:
Intercom
If you are using Intercom, the SDK will automatically track flags values for each user in custom data attributes. You simply have enabled the Intercom integration and choose which falgs to track. Just make sure the Intercom SDK is available on window.Intercom
.
If you are importing the Intercom SDK via a <script />
tag, you do not need to do anything. If you are using npm, you need to expose the Intercom
object to the window object like so:
Reference
TgglClient
The client used to query the Tggl API.
Read more about the Node.js client.
TgglProvider
Place this component at the root of your app. It gives your app access to the client and the state of the client.
client
Pass a TgglClient
that will be used to evaluate flags.
initialContext
Allows you to pass an initial value.
Updating the value of initialContext
will have no effect, use the useTggl
hook instead
onFlagEvaluation
This function is called everytime a flag is evaluated. It takes an object as only parameter with the following keys: slug
, ans value
.
useTggl
You can use this hook anywhere inside the <TgglProvider />
.
It is mostly useful for updating the context with setContext
and updateContext
.
client
Returns the client instance passed to <TgglProvider />
.
setContext
Override the context and triggers an API call to evaluate flags.
updateContext
Just like setContext
but keys are merged into the existing context.
This should be used if you wish to only update a few keys, leaving the rest unchanged.
useFlag
Only read the keys that you really need to avoid unnecessary re-renders.
value
The value of the flag or the default value if the flag is not active, is deleted, or in case of network errors.
loading
True when an API call is being performed.
error
The error thrown by the last API call if any.