Find the OS type and version
suggest changeThe first step is to import Platform from the ‘react-native’ package like so:
import { Platform } from 'react-native'
You can then read the OS with Platform.OS and use it in conditions. StyleSheet.create expects objects of style properties, not a bare number:
const styles = StyleSheet.create({
box: {
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!');
}
On iOS, Platform.Version is a string. Parse it before a numeric comparison. On Android it is already a number.
if (parseInt(Platform.Version, 10) >= 9) {
console.log('Running version higher than 8');
}
If the platform-specific logic is complex, you can split it into two files, for example:
MyTask.android.jsMyTask.ios.js
and require it using
const MyTask = require('./MyTask')
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents