Component - Platform-specific styles
This React Native component shows how to apply different styles depending on the platform (iOS or Android). It uses platform-specific style rules to make the UI look native on each device.
This React Native component shows how to apply different styles depending on the platform (iOS or Android). It uses platform-specific style rules to make the UI look native on each device.
View ├── Text └── Pressable
Platform.OS in React Native styles?Platform.OS doesPlatform.OS returns the current device's operating system, like 'ios' or 'android'.Platform.OS to stylingPlatform.select for styles in React Native?Platform.select usagePlatform.select takes an object with keys for platforms and returns the value for the current platform.Platform.select inside a style object with platform keys and values.const styles = {
backgroundColor: Platform.select({
ios: 'red',
android: 'yellow',
default: 'gray'
})
};Platform.select will pick the 'android' key value.Platform.select:
const styles = {
fontSize: Platform.select({
ios: 16,
android: 14
});
};Platform.select is correct, and keys ios and android are valid.Platform.select call inside the object, which is invalid syntax.Platform.OS === 'ios' to set shadowColor and shadowOpacity only on iOS, setting them to undefined or 0 on Android, effectively disabling shadow.Pass the test and this pattern moves unknown