Using the React SDK
Getting a Feature Flag with a React Hook
Inside a React component, use the useFlag
hook to retrieve a Feature Flag.
import {useFlag} from "appflags-react";
function ExampleComponent() {
const flag = useFlag("flag_key");
}
The useFlag
hook subscribes to Feature Flag changes and will update the returned flag
whenever your Feature Flag is modified.
info
The AppFlags client takes about 100ms to initialize. The useFlag
hook will return undefined
until the client is initialized.
Flag Properties
The Flag
object contains the following properties:
key
: the key identifier for the Feature Flagvalue
: the value of the active Variation, either aboolean
,number
, orstring
Accessing the JavaScript SDK
The React SDK is built on top of the JavaScript SDK. You can retrieve the full JavaScript client like this:
import AppFlags from "appflags-react";
Example repository
For a full example of using AppFlags in a React application, see https://github.com/AppFlags/example-react.