Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

flagit-react-sdk

viswasekaran717MIT2.0.3TypeScript support: included

Lightweight React SDK for feature flags with server-side evaluation and synchronous access - no Provider needed

feature-flags, feature-toggles, react, react-hooks, typescript, flagit, sdk, feature-management, experimentation, ab-testing, canary-release, progressive-rollout

readme

Flagit React SDK

A lightweight React SDK for feature flags with server-side evaluation, intelligent targeting, and zero configuration needed.

🌐 Get Started: https://www.flagit.app/

Features

  • Single API Call - Fetch and evaluate all flags in one request
  • Server-Side Evaluation - All rules evaluated on backend with portal configuration
  • Synchronous Access - No async/await needed after initialization
  • Smart Targeting - User-based, attribute-based, and percentage rollouts
  • Lightweight - ~20KB bundle size
  • TypeScript Support - Full type definitions included
  • Zero Config - Just initialize and use

Installation

npm install flagit-react-sdk

Quick Start

import flagit from 'flagit-react-sdk';

// 1. Initialize once at app startup
flagit.init({
  envKey: 'ff_development_yourproject_xxxxxxxxxxxx',
  userContext: {
    userId: 'user-123',
    email: 'user@example.com',
    attributes: {
      plan: 'premium',
      region: 'us-east'
    }
  }
});

// 2. Use anywhere in your app - NO ASYNC/AWAIT!
function MyComponent() {
  if (flagit.isEnabled('new-dashboard')) {
    return <NewDashboard />;
  }
  return <OldDashboard />;
}

API Reference

Initialization

flagit.init({
  envKey: string;              // Required: Environment key from dashboard
  apiUrl?: string;             // Optional: API URL (defaults to production)
  environment?: string;        // Optional: Override environment (development/production)
  userContext?: {              // Optional: User context for targeting
    userId?: string;
    email?: string;
    attributes?: Record<string, any>;
  };
  pollInterval?: number;       // Optional: Auto-refresh interval in ms (default: 30000)
  timeout?: number;            // Optional: Request timeout in ms (default: 5000)
  retryAttempts?: number;      // Optional: Retry attempts (default: 3)
  onError?: (error: Error) => void; // Optional: Error handler
});

flagit.isEnabled(flagName: string): boolean

// Simple boolean check - no await needed!
if (flagit.isEnabled('new-feature')) {
  // Feature is enabled
}

flagit.isFeatureEnabled(flagName: string): boolean

// Alias for isEnabled
const enabled = flagit.isFeatureEnabled('beta-feature');

flagit.getValue(flagName: string): any | null

// Get full flag object
const flag = flagit.getValue('my-feature');

flagit.isReady(): boolean

// Check if flags are loaded
if (flagit.isReady()) {
  // Flags are ready
}

User Context

flagit.setContext(context: UserContext): void

// Update user context dynamically
flagit.setContext({
  userId: 'user-456',
  email: 'new@example.com',
  attributes: { plan: 'enterprise' }
});

flagit.getContext(): UserContext | null

// Get current user context
const context = flagit.getContext();

Async Methods

flagit.refresh(): Promise<void>

// Manually refresh flags from server
await flagit.refresh();

flagit.startPolling(interval?: number): void

// Auto-refresh flags every 30 seconds
flagit.startPolling(30000);

flagit.stopPolling(): void

// Stop auto-refresh
flagit.stopPolling();

React Hooks

useFlagitClient

import { useFlagitClient } from 'flagit-react-sdk';

function MyComponent() {
  const client = useFlagitClient();

  // Access client methods
  const enabled = client.isEnabled('feature');
  client.setUserContext({ userId: '123' });

  return <div>...</div>;
}

useAllFeatureFlags

import { useAllFeatureFlags } from 'flagit-react-sdk';

function FlagList() {
  const { flags, loading, error, refresh } = useAllFeatureFlags();

  if (loading) return <div>Loading...</div>;
  if (error) return <div>Error: {error}</div>;

  return (
    <ul>
      {flags.map(flag => (
        <li key={flag.name}>{flag.name}: {flag.isActive ? '✓' : '✗'}</li>
      ))}
    </ul>
  );
}

Components

FeatureFlag

import { FeatureFlag } from 'flagit-react-sdk';

function App() {
  return (
    <FeatureFlag
      flag="new-feature"
      fallback={<OldFeature />}
      onError={(error) => console.error(error)}
    >
      <NewFeature />
    </FeatureFlag>
  );
}

How It Works

  1. Initialize once with flagit.init() - SDK fetches and evaluates all flags
  2. Server evaluates flags using rules defined in your portal
  3. Results cached locally for instant synchronous access
  4. Use anywhere with flagit.isEnabled() - no async needed!

Evaluation Flow

Client                          Backend
  |                               |
  |-- POST /sdk/flags ----------->|
  |   { context: {...} }          |
  |                               |
  |                        [Evaluate all flags]
  |                        [Apply portal rules]
  |                        [Check rollouts]
  |                               |
  |<-- { "flag1": true, ... } ----|
  |                               |
[Cache results]
  |
[flagit.isEnabled() → instant!]

Advanced Usage

Dynamic User Context

import flagit from 'flagit-react-sdk';

// Initialize without context
flagit.init({ envKey: 'xxx' });

// Set context after user logs in
function LoginPage() {
  const handleLogin = async (user) => {
    // Update context
    flagit.setContext({
      userId: user.id,
      email: user.email,
      attributes: {
        plan: user.subscription,
        role: user.role
      }
    });

    // Refresh flags with new context
    await flagit.refresh();
  };
}

Environment-Specific Configuration

const config = {
  envKey: import.meta.env.VITE_FLAGIT_ENV_KEY,
  environment: import.meta.env.MODE, // 'development' or 'production'
  apiUrl: import.meta.env.VITE_FLAGIT_API_URL
};

flagit.init(config);

Migration from Other SDKs

From LaunchDarkly

// Before (LaunchDarkly)
const client = await LDClient.initialize(clientKey, context);
const flagValue = client.variation('feature-key', false);

// After (Flagit)
flagit.init({ envKey: 'xxx', userContext: context });
const flagValue = flagit.isEnabled('feature-key'); // No await!

From Flagsmith

// Before (Flagsmith)
await flagsmith.init({ environmentID: 'xxx' });
const enabled = flagsmith.hasFeature('feature');

// After (Flagit)
flagit.init({ envKey: 'xxx' });
const enabled = flagit.isEnabled('feature'); // Same pattern!

Best Practices

  1. Initialize early - Call flagit.init() before rendering your app
  2. Set context once - Include user context in init config
  3. Use synchronous methods - Prefer isEnabled() over async methods
  4. Handle errors - Provide onError callback in config
  5. Refresh strategically - Use polling for long-lived apps

Troubleshooting

Flags not loading?

  • Check your environment key is correct
  • Verify network requests in browser DevTools
  • Check console for initialization errors

Always getting false?

  • Ensure flags are enabled in portal
  • Check user context matches targeting rules
  • Verify environment is correct

TypeScript errors?

  • Update to latest version
  • Check import paths are correct

Get Your Environment Key

  1. Sign up at https://www.flagit.app/
  2. Create a new project or select an existing one
  3. Navigate to SettingsDeveloper
  4. Copy the environment key for your environment (development or production)

License

MIT

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

[2.0.0] - 2025-11-18

🚀 Major Release - Complete Redesign

Breaking Changes

  • Removed FlagitProvider - No Provider wrapper needed, use singleton pattern
  • Removed useFeatureFlag hook - Use flagit.isEnabled() instead
  • Removed useFlagitContext - Use singleton directly
  • Removed custom client-side rules - All rules evaluated server-side only
  • Changed API endpoint - Single unified POST /sdk/flags endpoint
  • Removed multiple endpoints - Consolidated from 3 endpoints to 1

Added

  • Singleton pattern - flagit.init() and use anywhere
  • Synchronous access - flagit.isEnabled() with no async/await
  • Single API call - Fetch and evaluate all flags in one request
  • Server-side only evaluation - All rules managed in portal
  • User context in init - Pass context once during initialization
  • Smaller bundle - Reduced from 22KB to 19KB (~14% reduction)

Changed

  • Backend API: Single POST /sdk/flags endpoint handles everything
  • Response format: Returns { flagName: boolean } map directly
  • Initialization: Auto-fetches and evaluates flags with user context
  • Component: <FeatureFlag> now uses singleton (no loading state)
  • Hooks: Simplified to useFlagitClient() and useAllFeatureFlags()

Migration Guide

Before (v1.x)

<FlagitProvider config={{ envKey: 'xxx' }}>
  <App />
</FlagitProvider>

const { isEnabled, loading } = useFeatureFlag('feature');
if (loading) return <Loading />;

After (v2.0)

// Initialize once
flagit.init({ 
  envKey: 'xxx',
  userContext: { userId: '123' }
});

// Use anywhere - no loading!
if (flagit.isEnabled('feature')) {
  // ...
}

Performance

  • 🚀 Single API call vs multiple calls
  • 🚀 No Provider overhead
  • 🚀 Synchronous flag access (no re-renders)
  • 🚀 Smaller bundle size

Documentation

  • Complete README rewrite
  • Migration guide from v1.x
  • Comparison with LaunchDarkly/Flagsmith
  • Best practices guide

[1.1.2] - 2025-11-18

Changed

  • Default API URL set to production: https://flagit-server.onrender.com
  • apiUrl is now optional in config
  • Updated documentation to reflect optional apiUrl

Added

  • Custom client-side rules support via optional rules config
  • CustomRule type for defining client-side evaluation logic
  • Client-side rule evaluation executes before server-side rules
  • Enhanced documentation with custom rules examples

Important Notes

  • Custom rules are optional - SDK works without them
  • Rules must be passed in initial config only
  • Rules cannot be modified after SDK initialization

Improved

  • Better developer experience - only envKey is required
  • Reduced configuration complexity
  • Added examples for custom rules in QUICKSTART and README

[1.1.0] - 2025-11-18

Added

  • Complete SDK restructure with environment key authentication
  • Server-side rule evaluation with user context
  • User targeting support
  • Attribute-based targeting
  • Percentage-based rollouts with consistent hashing
  • TypeScript definitions
  • React hooks API

[1.0.0] - 2025-11-18

Added

  • Environment key-based authentication system
  • Server-side rule evaluation with user context
  • setUserContext() method for user targeting
  • evaluateFlag() method for server-side flag evaluation
  • Support for user targeting rules
  • Support for attribute-based targeting
  • Consistent hash-based percentage rollouts
  • useAllFeatureFlags() hook for fetching all flags
  • EvaluationResult interface with detailed evaluation info
  • UserContext interface for targeting

Changed

  • BREAKING: Replaced JWT authentication with environment keys
  • BREAKING: Removed projectId, accessToken, and refreshToken from config
  • BREAKING: Changed useFeatureFlags to accept array of flag names
  • BREAKING: Hook return values now include rollout and reason instead of config
  • Updated all API endpoints from /projects/* to /sdk/*
  • Authentication now uses x-flagit-env-key header instead of Bearer token
  • Flag evaluation moved from client-side to server-side

Removed

  • BREAKING: useToggleFeatureFlag hook (mutations should be done via dashboard)
  • BREAKING: useFeatureFlagRenderer hook (incompatible with async rendering)
  • BREAKING: environment parameter from most methods
  • BREAKING: config property from useFeatureFlag return value
  • JWT token refresh logic
  • Client-side flag evaluation

Security

  • Environment keys are read-only (no mutation capabilities)
  • Server-side rule validation prevents client-side tampering
  • Scoped keys per environment (development/production isolation)

[Unreleased]

Planned

  • Real-time flag updates via WebSocket
  • Offline mode with cached flags
  • React Native support
  • Advanced analytics integration
  • A/B testing utilities