--- title: How to set up A/B tests in React Native (Expo) date: 2024-03-01 author: - lior-neu-ner tags: - experimentation --- import { ProductScreenshot } from 'components/ProductScreenshot' export const EventsInPostHogLight = "https://res.cloudinary.com/dmukukwp6/image/upload/posthog.com/contents/images/tutorials/react-native-ab-tests/events-light.png" export const EventsInPostHogDark = "https://res.cloudinary.com/dmukukwp6/image/upload/posthog.com/contents/images/tutorials/react-native-ab-tests/events-dark.png" [A/B tests](/experiments) helps you improve your React Native app by enabling you to compare the impact of changes on key metrics. PostHog makes [A/B testing in React Native](/docs/experiments/installation?tab=React+Native) simple. To show you how to set it up, we create a basic app with Expo, add PostHog, create an A/B test, and implement the code for it. > This tutorial focuses on React Native apps built with [Expo](https://expo.dev/). However, PostHog also supports non-Expo apps. See our [React Native docs](/docs/libraries/react-native) for details. ## 1. Create a new React Native app Our app will have two screens: 1. The first screen will have a button which navigates you to a second screen. 2. The second screen will either have a `red` or `green` background color – depending on whether the user is in the `control` or `test` variant of our A/B test. It will also have a button which captures an event when it's pressed. We'll use this event as our goal metric for the test. First, ensure you have [Node (v14.0 or newer)](https://nodejs.dev/en/learn/how-to-install-nodejs/) and [Watchman](https://facebook.github.io/watchman/docs/install) installed. You also need the Expo Go app on either [Android](https://play.google.com/store/apps/details?id=host.exp.exponent&pli=1) or [iOS](https://apps.apple.com/app/expo-go/id982107779). Then create and run a new app: ```bash npx create-expo-app rn-ab-tests cd rn-ab-tests npx expo start ``` Open your Expo Go app on your mobile device, scan the QR code in your terminal, and it should open an app with a white screen and some text. ![Basic app](https://res.cloudinary.com/dmukukwp6/image/upload/v1710055416/posthog.com/contents/images/tutorials/react-native-analytics/basic.png) Next, we set up our app's basic functionality. To set up our stack navigator, we install `@react-navigation/native-stack` as a dependency and create a new file `AppNavigator.js` in our project directory. We also create two new files for our screens: ```bash npm install @react-navigation/native-stack touch AppNavigator.js touch FirstScreen.js touch SecondScreen.js ``` Add the following code to `AppNavigator.js`: ```js file=AppNavigator.js import { createNativeStackNavigator } from '@react-navigation/native-stack'; import FirstScreen from './FirstScreen'; import SecondScreen from './SecondScreen'; const Stack = createNativeStackNavigator(); function AppNavigator() { return ( ); } export default AppNavigator; ``` Then replace the code in `App.js` to use the navigator: ```js file=App.js import { NavigationContainer } from '@react-navigation/native'; import AppNavigator from './AppNavigator'; export default function App() { return ( ); } ``` Next we set up the code for our screens: ```js file=FirstScreen.js import { View, Button, StyleSheet } from 'react-native'; export default function FirstScreen({ navigation }) { return (