Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 71 ยท Styling and Layout

Platform-Specific Code

Write code that adapts to iOS and Android in React Native: Platform.OS, Platform.select, .ios and .android files, shadows versus elevation, fonts and native feel.

One codebase for two platforms is the promise of React Native, and for most screens it holds without any extra effort. But iOS and Android are different systems with different conventions: shadows are drawn in different ways, the system fonts differ, Android has a hardware or gesture back button, and users on each platform expect small things to feel familiar. React Native gives you three tools for the cases where you must branch: Platform.OS, Platform.select, and platform-specific files.

This lesson shows each tool, explains when to prefer which, and walks through the most common differences, shadows and fonts. The native code is read-only on this site; the live examples are stand-ins for how the selection and file resolution work.

Platform.OS

Platform.OS is a string that tells you where the code is running: 'ios' or 'android'. (Projects using react-native-web also see 'web', and community builds add 'windows' and 'macos'.) Use it in ordinary conditionals:

TSX
import { Platform, StatusBar } from 'react-native';

if (Platform.OS === 'android') {
  StatusBar.setBackgroundColor('#ffffff');
}

const headerHeight = Platform.OS === 'ios' ? 44 : 56;

Platform.Version gives the OS version: a string on iOS (for example "17.4") and a number, the API level, on Android. Compare them with care because their types differ: Number(Platform.Version) >= 33 is a typical Android check. A constant such as Platform.isPad (iOS only) and Platform.isTV also exist.

Platform and device details

Platform also carries more than the OS name:

TSX
import { Platform, Dimensions, PixelRatio } from 'react-native';

Platform.OS;                          // "ios" | "android"
Platform.Version;                     // "17.4" on iOS, API level (number) on Android
Platform.isPad;                       // iOS only: true on an iPad
Platform.isTV;                        // true on Apple TV and Android TV builds
Platform.constants.reactNativeVersion; // { major, minor, patch }
Platform.constants.Brand;             // Android: "google"
Platform.constants.Model;             // Android: "Pixel 8"
Platform.constants.systemName;        // iOS: "iOS" (iPadOS reports "iPadOS")

const { width, height } = Dimensions.get('window');   // size of the app window in points
const scale = PixelRatio.get();                       // physical pixels per point (2, 3, ...)

Platform.constants holds platform-specific facts, so check Platform.OS before reading Android-only fields such as Brand. For richer device information (model name, memory, whether it is a tablet, the app's version and build number) use Expo modules: expo-device (Device.modelName, Device.deviceType, Device.osVersion), expo-application (Application.nativeApplicationVersion) and expo-constants (Constants.expoConfig). Read the screen size with useWindowDimensions so it updates on rotation (see Dimensions and Responsive Layout).

Be careful with how you use this data. Branching on the model to fix a layout usually ends badly, because new devices appear every year; prefer measuring the window or testing for a capability. Use device details for logging, support and analytics, and never as a security check.

Platform.select

Branching on Platform.OS repeatedly gets noisy. Platform.select picks a value from an object keyed by platform, and it works for any value, including whole style objects:

TSX
import { Platform, StyleSheet } from 'react-native';

const styles = StyleSheet.create({
  title: {
    fontSize: 20,
    ...Platform.select({
      ios: { fontFamily: 'Menlo' },
      android: { fontFamily: 'monospace' },
      default: {},
    }),
  },
});

The default key covers any platform you did not list (web, for instance). If neither the platform nor default is present, the result is undefined, so spreading it into an object still works. Note that Platform.select evaluates at run time and the value for the other platform still ships in your bundle. For small values that costs nothing; for large chunks of code, use the file-based approach below.

Three levels of branching

Platform.OS A string for plain if statements.
Platform.select Choose one value, even a style.
File extensions One file per platform.

Start small. Reach for files only when the code really differs.

1 / 2

Platform-specific files

When a component needs different logic or even different native modules per platform, write two files with the platform in the name:

TEXT
components/
  DatePicker.ios.tsx
  DatePicker.android.tsx
  Screen.tsx

You import without any extension:

TSX
import { DatePicker } from './components/DatePicker';

The bundler (Metro) resolves DatePicker to DatePicker.ios.tsx when building for iOS and DatePicker.android.tsx when building for Android. Only the matching file is included in each bundle. A file ending in .native.tsx is used by both iOS and Android and wins over a plain .tsx, which is useful when a web build needs a different implementation. The resolution order is: platform file, then .native file, then the plain file.

Keep both implementations behind the same props and the same exports, so the rest of the app never knows the difference. TypeScript will not check that for you across two files, so a shared type for the props is a good habit.

The resolution logic is small enough to reproduce. This stand-in mimics the order Metro uses, so you can test what a file layout would pick:

JS Stand-in: which file does the bundler pick?

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The third line prints null: layout A has no file for web, and that is exactly the kind of gap that shows up as a "module not found" error in a real web build. Always provide a plain fallback file if any platform might be missing.

Shadows versus elevation

The classic difference. On iOS a shadow is built from four props:

TSX
const cardShadow = {
  shadowColor: '#000',
  shadowOffset: { width: 0, height: 2 },
  shadowOpacity: 0.15,
  shadowRadius: 6,
};

On Android those props do nothing; Android uses elevation, a single number that raises the view and draws a Material-style shadow:

TSX
const cardElevation = { elevation: 4 };

The usual pattern puts both in one Platform.select:

TSX
card: {
  backgroundColor: '#fff',
  borderRadius: 12,
  ...Platform.select({
    ios: { shadowColor: '#000', shadowOffset: { width: 0, height: 2 }, shadowOpacity: 0.15, shadowRadius: 6 },
    android: { elevation: 4 },
  }),
},

Two gotchas. On iOS, overflow: 'hidden' clips the shadow along with the content, so put the shadow on an outer view and the clipping on an inner one. On Android, elevation also changes the draw order. Newer React Native versions support a CSS-like boxShadow style on both platforms, which removes most of this branching; check the version your project uses before relying on it.

Fonts and native feel

iOS defaults to San Francisco and Android to Roboto, so text measures slightly differently. The built-in names also differ: 'Menlo' exists on iOS, 'monospace' on Android, and 'sans-serif' is an Android name only. A custom font avoids this: load the same font file on both platforms (covered in Theming and Dark Mode) and the text looks identical.

Beyond styles, plan for behavior:

  • Back navigation. Android has a system back action (button or gesture) that your screens must respect; iOS relies on the swipe from the left edge and the header's back button. Navigation libraries handle most of it, and Common Pitfalls and Fixes shows the cases that remain.
  • Touch feedback. Android shows a ripple, iOS usually fades opacity. Pressable supports both through android_ripple and style callbacks, see Pressable, Buttons and Touch Feedback.
  • Dialogs and pickers. Native date pickers look different on each platform; that is a good use case for .ios.tsx and .android.tsx files.
  • Permissions and status bar. Both work differently; prefer a library that hides the difference.

A guideline: let the platform look like itself where users expect it (pickers, back behavior, ripples) and share everything else (layout, data, branding). Do not fork whole screens "just in case".

A selector demo

This stand-in shows Platform.select at work. Pick a platform, and the same component renders its shadow and font values from one table, the way the real prop objects above do.

Stand-in: one component, two platforms

Choosing "web" shows the default branch for the font and no shadow at all, since that table has no default for the shadow: the same undefined result real Platform.select gives for a missing key.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which value does Platform.OS hold when the code runs on an iPhone?

    Platform.OS is 'ios' or 'android' (and 'web' under react-native-web). It is the same for iPhones and iPads.
  2. 2. You have Button.tsx and Button.ios.tsx. Which file does an Android build use for import './Button'?

    Metro looks for Button.android.tsx, then Button.native.tsx, then Button.tsx. On Android only the plain file matches here.
  3. 3. Which style gives a drop shadow on Android in classic React Native?

    Android draws shadows from elevation. The four shadow props are used by iOS. Newer versions also support boxShadow on both.
  4. 4. A card has borderRadius, overflow: 'hidden' and an iOS shadow, but no shadow appears on iOS. What is the likely fix?

    On iOS overflow: 'hidden' clips the shadow along with the content, so the shadow needs to live on a wrapper that does not clip.

Summary

  • Platform.OS is 'ios' or 'android'; Platform.Version is a string on iOS and an API-level number on Android.
  • Platform.select({ ios, android, default }) chooses a value or a whole style object at run time.
  • Files named .ios.tsx, .android.tsx and .native.tsx are resolved by Metro, which picks the platform file, then the native file, then the plain one; keep their props identical.
  • iOS shadows use four shadow* props, Android uses elevation; clip with an inner view so iOS shadows survive.
  • Let the platform look native for pickers, back behavior and touch feedback, and share the rest of the code.
  • The selectors and file resolver here are stand-ins; the real resolution happens in Metro at build time.

Next: Theming and Dark Mode