Introduction
StyleSheet helps make your app look good on both iPhone and Android without extra work.
StyleSheet helps make your app look good on both iPhone and Android without extra work.
import { StyleSheet } from 'react-native'; const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: 'white', padding: 10 }, text: { fontSize: 16, color: 'black' } });
const styles = StyleSheet.create({
button: {
backgroundColor: 'blue',
padding: 12
}
});const styles = StyleSheet.create({
title: {
fontSize: 24,
fontWeight: 'bold',
color: 'darkred'
}
});This app shows two text lines centered on the screen with consistent style on both platforms.
import React from 'react'; import { View, Text, StyleSheet } from 'react-native'; export default function App() { return ( <View style={styles.container}> <Text style={styles.title}>Hello, StyleSheet!</Text> <Text style={styles.subtitle}>Looks good on iOS and Android.</Text> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', alignItems: 'center', backgroundColor: '#f0f0f0' }, title: { fontSize: 28, fontWeight: '600', color: '#333' }, subtitle: { fontSize: 18, color: '#666', marginTop: 8 } });
StyleSheet helps React Native convert styles into native code for better speed.
Using StyleSheet prevents accidental inline style mistakes.
It also helps tools check your styles for errors before running the app.
StyleSheet creates styles that work well on both iOS and Android.
It improves app speed by optimizing styles behind the scenes.
It keeps your style code clean and easy to manage.
StyleSheet.create() when styling components?StyleSheet.create() helps write styles that behave the same on iOS and Android, avoiding platform differences.StyleSheet.create() in React Native?StyleSheet.create(), not StyleSheet(), createStyleSheet(), or StyleSheet.make().const styles = StyleSheet.create({ text: { fontSize: 20, color: 'red' } });
function App() {
return Hello!;
}const styles = StyleSheet.create({
button: {
backgroundColor: 'green',
flex: '1'
}
});flex property expects a number, but here it is given as a string '1'.flex causes a runtime warning or error; it must be a number like 1.StyleSheet.create() ensures platform-consistent UI?elevation for shadows, while iOS uses shadow properties like shadowColor, shadowOffset, and shadowOpacity.StyleSheet.create() ensures the button has shadows on both platforms consistently.Pass the test and this pattern moves unknown