React Native allows you to write a single codebase for both Android and iOS. However, some UI styles and behaviors need to change depending on the platform. Platform-specific styling helps you customize appearance and behavior for Android, iOS, and Web while keeping code clean and maintainable.
React Native exposes a built-in Platform module that tells you which platform your app is running on. You can use it inside JavaScript logic or directly inside styles.
// Import Platform module from react-native
import { Platform } from "react-native";
// Check current operating system
if (Platform.OS === "android") {
console.log("Running on Android");
} else if (Platform.OS === "ios") {
console.log("Running on iOS");
}
// Platform-specific styles using conditional logic
import { StyleSheet, Platform } from "react-native";
const styles = StyleSheet.create({
container: {
padding: Platform.OS === "android" ? 16 : 20,
backgroundColor: Platform.OS === "ios" ? "#f1f5f9" : "#e5e7eb"
}
});
export default styles;
On Android devices, the container gets slightly less padding and a different background color. On iOS, padding is larger to match Apple’s design guidelines. This ensures the UI feels native on both platforms.
Toggle the switch below to simulate how Platform.select() changes the appearance of a component. Observe how the Shadow, Radius, and Button Style adapt.
// Cleaner platform-specific styles using Platform.select
import { StyleSheet, Platform } from "react-native";
const styles = StyleSheet.create({
card: {
backgroundColor: 'white',
padding: 20,
...Platform.select({
ios: {
shadowColor: '#000',
shadowOffset: { width: 0, height: 10 },
shadowOpacity: 0.15,
borderRadius: 16,
},
android: {
elevation: 5,
borderRadius: 4,
borderWidth: 1,
borderColor: '#ddd'
}
})
}
});
Platform.select() for cleaner style definitions