React Router 7 with Vite
React Router is a standard library for routing in React applications that enables dynamic, client-side routing. This document explains how to integrate Flagship feature flag management with React Router 7 in a Vite-powered React application.
Project Overview
This project demonstrates how to integrate Flagship feature flagging system with a React Router 7 application. The integration allows you to:
Toggle features based on Flagship flags
Send analytics hits to track user behavior
Display different UI components based on feature flags
Route between different pages with flag-controlled behavior
Setup and Configuration
1. Flagship Provider Setup
The application uses a custom FsProvider component from FsProvider.tsx to initialize Flagship with your environment credentials:
// helpers/FsProvider.tsx
import FlagshipProvider, {
LogLevel,
type VisitorData,
} from "@flagship.io/react-sdk";
import { Loader } from "../components/Loader";
export function FsProvider({
children,
visitorData
}: {
children: React.ReactNode;
visitorData?: VisitorData;
}) {
return (
<FlagshipProvider
envId={import.meta.env.VITE_ENV_ID} // Environment ID from Flagship
apiKey={import.meta.env.VITE_API_KEY} // API Key from Flagship
logLevel={LogLevel.DEBUG}
visitorData={visitorData || null} // visitor data to pass
loadingComponent={<Loader />} // Optional Loading component while flags are being fetched
>
{children}
</FlagshipProvider>
);
}2. Application Structure
The main application structure in App.tsx sets up React Router and wraps everything in the Flagship provider:
3. Layout Component
The application uses a common layout with navigation in Layout.tsx:
4. Navigation Component
The navigation component in Nav.tsx uses React Router's Link component:
Using Flagship Flags
There are two main ways to access feature flags in your components:
Option 1: Use the useFsFlag Hook Directly
As shown in anotherPage.tsx:
Option 2: Create Reusable Flag Components
For better reusability, create dedicated components like MyFlagComponent.tsx:
Then use this component in your routes like home.tsx:
Sending Analytics Hits
To track user actions, you can send analytics hits using the Flagship SDK as shown in MyButtonSendHit.tsx:
Handling Loading States
The application includes a loading component in Loader.tsx that displays while Flagship flags are being fetched:
Environment Configuration
For Flagship to work properly, you need to set up the following environment variables in a .env.local file:
Running the Application
To run the application:
Additional Considerations
Visitor Data: You can customize the visitor data passed to Flagship in the
FsProvidercomponent.Default Values: Always provide default values for flags in case they can't be retrieved from Flagship.
Error Handling: Consider adding error boundaries around components that depend on feature flags.
Performance: Be mindful of how many flag lookups you're performing in frequently re-rendered components.
For more information about Flagship, refer to the official documentation.
Last updated
Was this helpful?

