Getting Started
Installation
Section titled “Installation”Install the package using your preferred package manager:
npm install @localflag/react# orpnpm add @localflag/react# oryarn add @localflag/reactDefine Your Flags
Section titled “Define Your Flags”Create a file to define your feature flags with their default values. Use the defineFlags helper for type safety:
import { defineFlags } from '@localflag/react';
export const defaultFlags = defineFlags({ darkMode: false, newDashboard: true, experimentalFeature: false, maxItems: 10, apiVersion: "v1",});
export type AppFlags = typeof defaultFlags;The defineFlags function is a type-safe helper that ensures your flags have valid values (boolean, string, or number). It has no runtime overhead—it simply returns your flags object as-is.
Set Up the Provider
Section titled “Set Up the Provider”Wrap your application with the FeatureFlagProvider:
import { StrictMode } from 'react';import { createRoot } from 'react-dom/client';import { FeatureFlagProvider, LocalFlagDevTools } from '@localflag/react';import { defaultFlags } from './flags';import App from './App';
createRoot(document.getElementById('root')!).render( <StrictMode> <FeatureFlagProvider defaultFlags={defaultFlags}> <App /> <LocalFlagDevTools /> </FeatureFlagProvider> </StrictMode>);Use Feature Flags
Section titled “Use Feature Flags”Now you can use feature flags anywhere in your app:
import { useFeatureFlag, useFeatureFlagValue, FeatureFlag } from '@localflag/react';import type { AppFlags } from './flags';
function MyComponent() { // Check if a flag is enabled (boolean) const darkMode = useFeatureFlag<AppFlags>('darkMode');
// Get the raw value of a flag const maxItems = useFeatureFlagValue<AppFlags>('maxItems');
return ( <div className={darkMode ? 'dark' : 'light'}> <p>Showing up to {maxItems} items</p>
{/* Conditional rendering with FeatureFlag component */} <FeatureFlag<AppFlags> flag="newDashboard"> <NewDashboard /> </FeatureFlag> </div> );}Next Steps
Section titled “Next Steps”- Learn about the LocalFlagDevTools for local development
- Try the VS Code Extension to manage flags in your editor
- Explore the API Reference for all available options