1. React Native tutorial

Platform Module in react native

Find the OS Type/Version

The first step is to import Platform from the ‘react-native’ package like so:

import { Platform } from 'react-native'

After you’ve done that, you can go ahead and access the OS type through Platform.OS allowing you to use it in
conditional statements like

const styles = StyleSheet.create({
height: (Platform.OS === 'ios') ? 200 : 100,
})

If you want to detect the Android version, you can use Platform.Version like so:

if (Platform.Version === 21) {
console.log('Running on Lollipop!');
}

For iOS, Platform.Version is returning a String, for complex condition don’t forget to parse it.

if (parseInt(Platform.Version, 10) >= 9) {
console.log('Running version higher than 8');
}

If the platform specific logic is complex, one can render two different files based on platform. Ex:

MyTask.android.js
MyTask.ios.js

and require it using

const MyTask = require('./MyTask')

If you like this post, don’t forget to share 🙂

This article is written by our awesome writer
Comments to: Platform Module in react native

Your email address will not be published. Required fields are marked *

Attach images - Only PNG, JPG, JPEG and GIF are supported.

New Dark Mode Is Here

Sign In to access the new Dark Mode reading option.

Join our Newsletter

Get our monthly recap with the latest news, articles and resources.

By subscribing you agree to our Privacy Policy.

Latest Articles

Explore Tutorials By Categories

About

Codeverb is simply an all in one interactive learning portal, we regularly add new topics and keep improving the existing ones, if you have any suggestions, questions, bugs issue or any other queries you can simply reach us via the contact page

Login

Welcome to Codeverb

Ready to learn something new?
Join Codeverb!

Read Smart, Save Time
  •  
    Strength indicator
  •  
  •  
    Log In | Lost Password