Different devices may need different looks. Platform-specific styles help your app look right on each device.
Platform-specific styles in React Native
import { Platform, StyleSheet } from 'react-native'; const styles = StyleSheet.create({ container: { padding: Platform.OS === 'ios' ? 20 : 10, backgroundColor: Platform.select({ ios: 'lightblue', android: 'lightgreen', default: 'white' }) } });
Platform.OS returns the current platform as a string ('ios' or 'android').
Platform.select() helps choose values for each platform easily.
padding: Platform.OS === 'ios' ? 20 : 10
Platform.select to pick colors for each platform.backgroundColor: Platform.select({ ios: 'blue', android: 'green' })fontFamily: Platform.OS === 'android' ? 'Roboto' : 'Arial'
This app shows a centered text with background color and padding that change depending on the platform. The text color also changes.
import React from 'react'; import { View, Text, StyleSheet, Platform } from 'react-native'; export default function App() { return ( <View style={styles.container}> <Text style={styles.text}>Hello from {Platform.OS}!</Text> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: Platform.select({ ios: '#add8e6', android: '#90ee90', default: '#ffffff' }), padding: Platform.OS === 'ios' ? 30 : 15 }, text: { fontSize: 20, color: Platform.OS === 'ios' ? 'navy' : 'darkgreen' } });
Always test your app on both iOS and Android devices or simulators to see platform-specific styles in action.
Use Platform.select for cleaner code when choosing multiple style values.
Remember to provide a default value in Platform.select for other platforms or web.
Platform-specific styles let your app look good on different devices.
Use Platform.OS or Platform.select to set styles conditionally.
Test on all platforms to ensure your styles work well everywhere.
Practice
Platform.OS in React Native styles?Solution
Step 1: Understand what
Platform.OSdoesPlatform.OSreturns the current device's operating system, like 'ios' or 'android'.Step 2: Connect
Developers use it to apply styles that look good on each platform, adjusting UI differences.Platform.OSto stylingFinal Answer:
To apply different styles depending on the device's operating system -> Option AQuick Check:
Platform.OS = conditional styles [OK]
- Thinking Platform.OS detects screen size
- Confusing it with language settings
- Using it to get device hardware info
Platform.select for styles in React Native?Solution
Step 1: Recall
Platform.selectusagePlatform.selecttakes an object with keys for platforms and returns the value for the current platform.Step 2: Check syntax correctness
const styles = { color: Platform.select({ ios: 'blue', android: 'green' }) }; correctly usesPlatform.selectinside a style object with platform keys and values.Final Answer:
const styles = { color: Platform.select({ ios: 'blue', android: 'green' }) }; -> Option AQuick Check:
Platform.select = object with platform keys [OK]
- Passing a ternary string instead of an object
- Using Platform.OS as a function
- Duplicating keys without platform names
const styles = {
backgroundColor: Platform.select({
ios: 'red',
android: 'yellow',
default: 'gray'
})
};Solution
Step 1: Identify the platform
The device is Android, soPlatform.selectwill pick the 'android' key value.Step 2: Check the value for Android
The value for 'android' is 'yellow', so the background color will be yellow.Final Answer:
yellow -> Option BQuick Check:
Android platform picks 'yellow' [OK]
- Choosing the default value instead of platform key
- Confusing iOS and Android values
- Assuming undefined if platform key exists
Platform.select:
const styles = {
fontSize: Platform.select({
ios: 16,
android: 14
});
};Solution
Step 1: Check syntax of Platform.select usage
The object passed toPlatform.selectis correct, and keys ios and android are valid.Step 2: Identify syntax error
There is an extra semicolon after thePlatform.selectcall inside the object, which is invalid syntax.Final Answer:
Extra semicolon after Platform.select call -> Option CQuick Check:
Semicolon inside object literal is syntax error [OK]
- Thinking default key is mandatory
- Assuming fontSize must be string
- Believing Platform.select can't be in styles
Solution
Step 1: Understand shadow properties on platforms
iOS uses shadowColor and shadowOpacity for shadows; Android uses elevation. To have no shadow on Android, shadow properties should be disabled or undefined.Step 2: Analyze each option for correct conditional styling
const buttonStyle = { shadowColor: Platform.OS === 'ios' ? '#000' : undefined, shadowOpacity: Platform.OS === 'ios' ? 0.5 : 0 }; usesPlatform.OS === 'ios'to set shadowColor and shadowOpacity only on iOS, setting them to undefined or 0 on Android, effectively disabling shadow.Final Answer:
const buttonStyle = { shadowColor: Platform.OS === 'ios' ? '#000' : undefined, shadowOpacity: Platform.OS === 'ios' ? 0.5 : 0 }; -> Option DQuick Check:
Use Platform.OS check for conditional shadow [OK]
- Setting Android shadowColor instead of disabling
- Using elevation without Android values
- Not disabling shadowOpacity on Android
Ready to prove it?
Pass the test and this pattern moves unknown
