Roadmap to becoming a developer in 2022
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

1.6 KiB

Platform Module

The Platform module, as the name suggests, is a part of React Native that detects the platform on which the app is running. This enables you to have specific code for either Android or iOS, allowing you to account for platform-specific differences in design or behavior.

To utilize the Platform module, you need to import it and then access the OS property. This property returns a string, which denotes the platform — either 'ios' or 'android'.

Here's an example:

import { Platform } from 'react-native';

if (Platform.OS === 'ios') {
  console.log('Running on iOS');
} else if (Platform.OS === 'android') {
  console.log('Running on Android');
}

For a more elegant way to define platform-specific properties, React Native provides the Platform.select method. This method takes an object with keys 'ios' and 'android', representing the respective platforms, and returns the value associated with the current platform.

Here's an example of Platform.select in use:

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

const styles = StyleSheet.create({
  container: {
    ...Platform.select({
      ios: {
        backgroundColor: 'red',
      },
      android: {
        backgroundColor: 'blue',
      },
    }),
  },
});

In this example, the container's background color will be red on iOS and blue on Android.

With the Platform module, you can easily create platform-specific code, enabling you to have the best user experience for each platform. Just remember to import the module and use the provided properties and methods.