Skip to main content

Platform

Example​

React JSX
import { Platform, StyleSheet, Text, ScrollView } from 'react-native';

const App = () => {
return (
<ScrollView contentContainerStyle={styles.container}>
<Text>OS</Text>
<Text style={styles.value}>{Platform.OS}</Text>
<Text>System name</Text>
<Text style={styles.value}>{Platform.constants.systemName}</Text>
<Text>JavaScript engine</Text>
<Text style={styles.value}>
{JSON.stringify(Platform.constants.jsEngine)}
</Text>
<Text>Constants</Text>
<Text style={styles.value}>
{JSON.stringify(Platform.constants, null, 2)}
</Text>
</ScrollView>
);
};

const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
value: {
fontWeight: '600',
padding: 4,
marginBottom: 8,
},
});

export default App;

Reference

Properties​

constants​

React TSX
static constants: PlatformConstants;

Returns an object which contains the constants related to the platform.

Properties:

NameTypeDescription
reactNativeVersionobjectInformation about the React Native version. Keys are major, minor, patch with optional prerelease.
systemNamestringThe operating system: 'macOS', 'Windows' or 'Linux'.
jsEngineobjectThe JavaScript engine, with the keys name and version. See JavaScript engines.
interfaceIdiomstringAlways 'desktop'.
isTestingbooleanWhether the app runs under the end-to-end test driver.
isDisableAnimationsbooleanWhether animations are turned off. When not set, it follows isTesting.
osVersionstringThe operating system version, in the format described under Version.

isDisableAnimations​

React TSX
static isDisableAnimations: boolean;

Returns true when animations are turned off, usually during automated tests started with disableAnimations. Falls back to isTesting if not set.

Type
boolean

isTesting​

React TSX
static isTesting: boolean;

Returns a boolean which defines if the application runs under the end-to-end test driver. See End-to-end testing.

Type
boolean

OS​

React TSX
static OS: 'macos' | 'windows' | 'linux';

Returns a string with the operating system the app runs on. The same JavaScript bundle runs on all three, so the value is read at run time and not substituted when the bundle is built.

Type
enum('macos', 'windows', 'linux')

Version​

React TSX
static Version: string;

The version of the operating system: the product version on macOS (15.1), major.minor.build on Windows (10.0.26100), and the distribution's VERSION_ID on Linux (24.04), falling back to the kernel release. To branch on what the platform can do, check capabilities() instead.

Type
string

Methods​

select()​

React TSX
static select(config: Record<string, T>): T;

Returns the most fitting value for the platform you are currently running on.

Parameters:​

NameTypeRequiredDescription
configobjectYesSee config description below.

The config parameter is an object with the following keys, checked in this order:

  • macos, windows or linux (any): the key of the current operating system
  • desktop (any): all three desktop operating systems
  • native (any)
  • default (any)

Example usage:

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

const styles = StyleSheet.create({
container: {
flex: 1,
...Platform.select({
macos: {
backgroundColor: 'red',
},
windows: {
backgroundColor: 'green',
},
default: {
// other platforms, such as Linux
backgroundColor: 'blue',
},
}),
},
});