Platform.OS, Platform.select and Platform Files

+15 Mana ✨

Most of a React Native screen runs unchanged on iOS and Android, but a font weight, a native color or a whole date picker sometimes has to differ. You have two tools for that: the Platform module for small differences inside one file, and platform-specific file extensions for components whose code diverges. Using the wrong one leaves you with either a file buried in conditionals or two copies of code that should have stayed shared.

Key terms

  • Platform: the module from react-native that tells you which platform the app is running on and picks values per platform.
  • Platform.select: a method that takes an object keyed by platform and returns the most fitting value for the current one.
  • Platform.Version: the operating system version, an API level number on Android and a version string on iOS.
  • Platform-specific extensions: the .ios. and .android. parts of a file name that React Native uses to load one file per platform.

Small differences stay in one file

Reach for the Platform module when only small parts of a component change per platform. Platform.OS is 'ios' on iOS and 'android' on Android, which covers a one-off condition in JSX or a guard around something only one platform has.

When every platform needs its own value, Platform.select reads better than a chain of conditions. Its keys can be ios, android, native and default, and it returns one value following a fixed priority:

  • On iOS, the ios key wins. Without it, native is used, then default.
  • On Android, the order is the same with android first.
  • When none of those keys is present, the result is undefined.

The order you write the keys in doesn't matter: an iPhone takes ios even when native comes first in the object. native covers both iOS and Android, while default also serves other platforms, web for example. The values can be anything, from numbers and style objects to components.

If you build the object dynamically, keep in mind that Platform.select checks whether a key is present with the in operator. It never looks at whether the value is defined, so Platform.select({ ios: undefined, default: 16 }) returns undefined on an iPhone because the ios key exists.

API levels and version strings

Platform.Version means something different on each platform. On Android it is the API level, a number such as 34, and not the Android release number users know. Android 10 is API level 29, so Platform.Version === 10 compares against API level 10 and does not detect Android 10. When you need the release string for display or logging, read Platform.constants.Release.

On iOS, Platform.Version is a string such as '26.0', taken from the device's system version. To compare major versions, parse it first with parseInt(Platform.Version, 10).

The Toolbar example below uses both. It checks whether the system has a dark theme setting for the color scheme to follow, which means Android 10 (API level 29) and higher, or iOS 13 and higher, and it picks a title weight per platform.

The Platform.OS === 'android' check comes first, so Platform.Version is compared as a number only where it is one, and the iOS branch parses the string. In the styles, ...Platform.select({ ios: ..., android: ..., default: ... }) spreads one platform's entries into title, and the default key keeps the style complete on any other platform.

One component, two files

When the platform code grows past a few branches, split it into separate files. DatePickerField.ios.tsx and DatePickerField.android.tsx sit next to each other and hold the two versions, and the screen imports the component without the platform part:

tsx
import DatePickerField from './DatePickerField';

React Native detects the .ios. or .android. extension and loads the right file for the platform it is running on. Give both files the same props, since every import site is shared between them.

A .native. file helps when the module is also shared with a React web app. The bundler picks DatePickerField.native.tsx for both Android and iOS, and a plain DatePickerField.tsx in the same folder is left for web bundlers.

Code Examples

tsx
import { Platform, StyleSheet, Text, View } from 'react-native';

// Android 10 is API level 29. iOS reports a string such as '26.0'.
const hasSystemDarkTheme =
  Platform.OS === 'android'
    ? Platform.Version >= 29
    : Platform.OS === 'ios' && parseInt(Platform.Version, 10) >= 13;

export function Toolbar({ title }: { title: string }) {
  return (
    <View style={styles.toolbar}>
      <Text style={styles.title}>{title}</Text>
      {hasSystemDarkTheme && <Text style={styles.note}>Follows your system theme</Text>}
    </View>
  );
}

const styles = StyleSheet.create({
  toolbar: { paddingHorizontal: 16, paddingVertical: 12 },
  title: {
    fontSize: 18,
    ...Platform.select({
      ios: { fontWeight: '600' },
      android: { fontWeight: '700' },
      default: { fontWeight: '500' },
    }),
  },
  note: { fontSize: 13, marginTop: 4 },
});
✓ Completed