While writing cross platform applications we need to add platform specific styles. There are many ways of accomplishing this.
React Native
introduced
Platform.select helper in
version 0.28
which allows us to write platform specific styles in a concise way.
In this blog we will see how to use this newly introduced feature.
A basic stylesheet file might look something like this.
import { StyleSheet } from "react-native";
export default StyleSheet.create({
container: {
flex: 1,
},
containerIOS: {
padding: 4,
margin: 2,
},
containerAndroid: {
padding: 6,
},
});
Now let's see how we can re-write this StyleSheet using Platform.select.
import { StyleSheet, Platform } from "react-native";
export default StyleSheet.create({
container: {
flex: 1,
...Platform.select({
ios: {
padding: 4,
margin: 2,
},
android: {
padding: 6,
},
}),
},
});

Written byBilal Budhani
Related blogs
- 01Running React Native animations on the UI threadMarch 13, 2023 · Sangamesh Somawar
- 02Removing the white screen before the Android splash screenJuly 28, 2022 · Kamolesh Mondal
- 03Playing videos in React NativeSeptember 14, 2021 · Chirag Bhaiji
- 04Handling environment specific configurations in React NativeApril 27, 2021 · Sourav Kumar