As a React Native developer, you've probably shaken your device countless times to access the developer menu. But did you know you can customize it with your own actions? In this tutorial, we'll explore the DevSettings API and how it can supercharge your development workflow.
Prerequisite
To follow along with this tutorial, you should have:
- Basic knowledge of React Native.
- A React Native project set up (either with Expo or React Native CLI).
- A physical device or emulator/simulator for testing.
What's a Dev Menu?
The React Native developer menu is a built-in debugging tool that provides quick access to common development actions like reloading the app, opening the debugger, or inspecting elements. It's only available in development builds and is hidden in production.
Opening the Dev Menu
There are several ways to open the developer menu:
- iOS Simulator: Press
Cmd + Dor shake the device - Android Emulator: Press
Cmd + M(macOS) orCtrl + M(Windows/Linux) - Physical Device: Shake the device
Once opened, you'll see options like "Reload", "Open Debugger", "Show Element Inspector", and more.
Feature 1: Add Custom Items to Dev Menu
One of the most powerful features of DevSettings is the ability to add custom menu items. This is incredibly useful for switching between development and production API endpoints without modifying environment variables.
Why is this better than process.env? Sometimes you just want to quickly test something against production endpoints without rebuilding your app or changing configuration files. With a custom menu item, it's just a tap away.
import React, { useEffect, useState } from "react";
import { View, Text, DevSettings } from "react-native";
const API_URLS = {
dev: "https://your-app.ngrok.io/api",
production: "https://api.yourapp.com",
};
export default function App() {
const [apiUrl, setApiUrl] = useState(API_URLS.dev);
useEffect(() => {
if (__DEV__) {
DevSettings.addMenuItem("Switch to Production API", () => {
setApiUrl(API_URLS.production);
alert("Now using Production API");
});
DevSettings.addMenuItem("Switch to Dev API (ngrok)", () => {
setApiUrl(API_URLS.dev);
alert("Now using Dev API");
});
}
}, []);
return (
<View style={{ flex: 1, justifyContent: "center", alignItems: "center" }}>
<Text>Current API: {apiUrl}</Text>
</View>
);
}
Now when you open the developer menu, you'll see your custom options at the bottom. This approach is much faster than updating .env files and rebuilding.
Note: The
__DEV__check ensures this code only runs in development mode and won't affect your production build.
Feature 2: Programmatic App Reload
Another handy feature is the ability to reload your app programmatically using DevSettings.reload(). This is useful when you want to reset the app state after certain actions, like clearing cache or switching user accounts during testing.
import React from "react";
import { View, Button, DevSettings, Alert } from "react-native";
import AsyncStorage from "@react-native-async-storage/async-storage";
export default function SettingsScreen() {
const handleClearCacheAndReload = async () => {
try {
await AsyncStorage.clear();
Alert.alert("Cache Cleared", "App will reload now.", [
{
text: "OK",
onPress: () => {
if (__DEV__) {
DevSettings.reload();
}
},
},
]);
} catch (error) {
Alert.alert("Error", "Failed to clear cache.");
}
};
return (
<View style={{ flex: 1, justifyContent: "center", padding: 20 }}>
<Button
title="Clear Cache & Reload"
onPress={handleClearCacheAndReload}
/>
</View>
);
}
This pattern is especially useful during development when you need to frequently reset the app state to test onboarding flows, authentication, or fresh installs.
Conclusion
The DevSettings API is a simple yet powerful tool that can significantly improve your React Native development experience. By adding custom menu items and leveraging programmatic reloads, you can create shortcuts for common development tasks and switch between configurations instantly.