platform modules in React Native


What is the use of platform modules in React Native?

React Native provides a module that detects the platform in which the app is running. You can use the detection logic to implement platform-specific code and use this option when only small parts of a component are platform-specific.

React Native provides two ways to organize your code and separate it by Platform:

  • Using the Platform module.
  • Using platform-specific file extensions.

Platform module

  • React Native provides a module that detects the platform in which the app is running. You can use the detection logic to implement platform-specific code and use this option when only small parts of a component are platform-specific.
  • Platform.OS will be ios when running on iOS and AndroidAndroid when running on AndroidAndroid.
  • There is also a Platform. Select the method available that is given an object containing Platform.OS as keys returns the value for the Platform you are currently running on.

Detecting the Android Version

On AndroidAndroid, the Platform module can also be used to detect the version of the Android Platform in which the app is running:

Detecting the iOS Version

On iOS, the Version is a result of -[UIDevice systemVersion], which is a string with the current version of the operating system.

Platform-specific extensions

When your platform-specific code is more complex, you should consider splitting the code out into separate files. React Native will detect when a file has a .ios. or .android. extension and load the relevant platform file when required from other components.