---
title: How to set up React feature flags with Vite
date: 2025-03-07
author:
- ian-vanagas
showTitle: true
sidebar: Docs
tags:
- feature flags
---
[Feature flags](/docs/feature-flags) help you release features and conditionally show content in your React apps. This tutorial shows you how to create a React app with Vite, add PostHog, create a feature flag, and then implement the flag to control content in your app.
## Create your React app with Vite
First, we create our React app using Vite and go into the newly created `react-flags` folder.
```bash
npm create vite@latest react-flags -- --template react
cd react-flags
npm install
```
We then remove the boilerplate code in `src/App.jsx` to simplify it to just a title.
```jsx
// src/App.jsx
import './App.css'
function App() {
return (
Welcome to my React app
)
}
export default App
```
Finally, run `npm run dev` and go to `http://localhost:5173` to see our new homepage.

## Adding PostHog
Since PostHog handles the management and evaluation of feature flags, we must set it up in our app. To do this, start by installing the `posthog-js` and `@posthog/react` libraries to get access to the [React SDK](/docs/libraries/react).
```bash
npm install posthog-js @posthog/react
```
Once installed, import PostHog into `src/main.jsx` and set it up using your project API key and host from [your project settings](https://us.posthog.com/settings/project). Wrap your app in the React `PostHogProvider` to access PostHog in any component.
```jsx
// src/main.jsx
import React from 'react'
import ReactDOM from 'react-dom/client'
import './index.css'
import App from './App.jsx'
import posthog from 'posthog-js'
import { PostHogProvider } from '@posthog/react'
posthog.init('', {
api_host: '',
defaults: '',
})
ReactDOM.createRoot(document.getElementById('root')).render(
,
)
```
Once done, start your app again with `npm run dev` and you should see an event autocaptured into PostHog.
## Creating a feature flag
With PostHog set up, your React app is ready for feature flags.
To create one, go to the [feature flags tab](https://app.posthog.com/feature_flags) in PostHog and click **New feature flag**. Enter a flag key (like `cool-react-homepage`), set the release condition to roll out to 100% of users, and press **Save**.
You can customize your conditions with percentage and person or group properties to fit your needs.
## Adding our feature flag
Once created, we can add our feature flag to our React app. We do this using the `useFeatureFlagEnabled` hook to conditionally show new content in our component.
```jsx
// src/App.jsx
import './App.css'
import { useFeatureFlagEnabled } from '@posthog/react'
function App() {
const flagEnabled = useFeatureFlagEnabled('cool-react-homepage')
return (
{flagEnabled ?
Welcome to my cool new React app
:
Welcome to my React app
}
)
}
export default App
```
With the flag enabled, our app now shows "Welcome to my cool new React app."

> **Want to remove the flicker while loading?** Read our tutorial on [How to bootstrap feature flags in React with Vite and Express](/tutorials/bootstrap-feature-flags-react).
### Using the PostHog feature component
An alternate way to implement feature flags is to use the `PostHogFeature` React component. This simplifies the logic of using flags as well as captures related usage automatically (such as a `$feature_view` event). We set the old content as the fallback for the component.
```jsx
// src/App.jsx
import './App.css'
import { PostHogFeature } from '@posthog/react'
function App() {
return (
Welcome to my React app}
>
Welcome to my cool new React app
)
}
export default App
```
These are basic implementations of React feature flags setup. From here, you can set up [A/B tests](/experiments), a [public beta program](/tutorials/public-beta-program), or [canary releases](/tutorials/canary-release).
## Further reading
- [Testing frontend feature flags with React, Jest, and PostHog](/tutorials/test-frontend-feature-flags)
- [How to add popups to your React app with feature flags](/tutorials/react-popups)
- [How to bootstrap feature flags in React with Vite and Express](/tutorials/bootstrap-feature-flags-react)