react-native-featureflags
v1.7.6
Published
React Native SDK for Truflag feature flags
Readme
react-native-featureflags
Truflag's React Native SDK for client-side feature flags.
Use it to evaluate flags, target users with attributes, perform a/b testing, and roll out changes gradually without shipping a new build.
Install
npm install react-native-featureflags @react-native-async-storage/async-storageThis package uses AsyncStorage for client persistence.
Quick start
import { useEffect } from "react";
import { useFlag, useFlagsReady } from "react-native-featureflags";
import Flags from "react-native-featureflags";
export default function App() {
useEffect(() => {
void Flags.configure({
apiKey: process.env.EXPO_PUBLIC_TRUFLAG_CLIENT_KEY!,
});
}, []);
return <CheckoutGate />;
}
function CheckoutGate() {
const ready = useFlagsReady();
const checkoutV2 = useFlag("checkout-v2", false);
if (!ready) return null;
return checkoutV2 ? <CheckoutV2 /> : <CheckoutV1 />;
}Common tasks
Prefer instance-based naming (consistent with native SDKs):
import { TruflagClient } from "react-native-featureflags";
const client = new TruflagClient();
await client.configure({ apiKey: process.env.EXPO_PUBLIC_TRUFLAG_CLIENT_KEY! });The existing singleton Flags API is still fully supported.
Log in a user and update attributes:
import Flags from "react-native-featureflags";
await Flags.login({
id: "user-123",
attributes: {
plan: "pro",
country: "US",
},
});
await Flags.setAttributes({
role: "member",
});Read flag values:
import Flags from "react-native-featureflags";
const welcomeCopy = Flags.getFlag("welcome-copy", "default");
const payload = Flags.getFlagPayload("welcome-copy");
const allFlags = Flags.getAllFlags();Track an event:
import Flags from "react-native-featureflags";
await Flags.track("checkout_completed", {
orderId: "ord_123",
value: 49.99,
currency: "USD",
});Reset to an anonymous user:
await Flags.logout();Docs
- Getting started: https://truflag.com/docs/getting-started
- React Native SDK guide: https://truflag.com/docs/sdks/react-native
- API reference: https://truflag.com/docs/api
