Platform-Specific Code
There are two ways to write code for one platform. File extensions
(Button.ios.tsx, Button.android.tsx) let the bundler (Metro) pick the right
file at build time; use them when the implementations differ a lot. The
Platform module (Platform.OS, Platform.select, Platform.Version)
branches at runtime; use it for small differences.
Applies to: React Native 0.88, Expo SDK 58.
Details
File Extensions
| File | Picked up by |
|---|---|
Component.ios.tsx | iOS build |
Component.android.tsx | Android build |
Component.native.tsx | iOS and Android, when there is no .ios / .android file |
Component.web.tsx | Web build (React Native Web) |
Component.tsx | Any platform without a more specific file |
Import without the extension: import Component from './Component'. The other
platform's file is not included in the bundle.
Platform Module
| API | What it gives |
|---|---|
Platform.OS | 'ios', 'android', 'web', … |
Platform.select({}) | The value for the current platform; keys ios, android, native, web, default |
Platform.Version | Android: API level as a number (34); iOS: system version as a string ('26.0') |
Platform.isPad | iOS only: the device is an iPad |
Example
import { Platform, StyleSheet, PermissionsAndroid } from 'react-native';
const styles = StyleSheet.create({
code: {
padding: Platform.OS === 'ios' ? 20 : 10,
fontFamily: Platform.select({
ios: 'Menlo',
android: 'monospace',
default: 'monospace',
}),
},
});
// Runtime permission for notifications exists since Android 13 (API 33)
async function askNotificationPermission() {
if (Platform.OS === 'android' && Platform.Version >= 33) {
await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.POST_NOTIFICATIONS
);
}
}
Follow-up Questions
- What ends up in the bundle? Only the matching platform file; in release builds
Metro also removes
Platform.OSandPlatform.selectbranches for other platforms whenPlatformis imported directly fromreact-native. - Where does native configuration go in an Expo app? The
iosandandroidkeys and config plugins inapp.json, not hand-editedios/andandroid/folders.
Common Mistakes
Platform.selectwithoutdefault: it returnsundefinedon web and other platforms.