Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Lesson 6 of 10
StyleSheet API - Performance and Patterns
StyleSheet is React Native's styling system. It looks like CSS but runs as JavaScript objects. Understanding how it works prevents performance issues and enables consistent design systems.
StyleSheet.create vs Inline Styles
import { StyleSheet, View, Text } from 'react-native';
// ❌ Inline style object - new object created on EVERY render
function Card() {
return <View style={{ backgroundColor: '#fff', padding: 16, borderRadius: 8 }} />;
}
// ✅ StyleSheet.create - styles registered once, referenced by ID
// The bridge sends integers (style IDs) instead of full objects each render
const styles = StyleSheet.create({
card: { backgroundColor: '#fff', padding: 16, borderRadius: 8 },
title: { fontSize: 18, fontWeight: '700', color: '#1a1a1a' },
body: { fontSize: 14, color: '#666', lineHeight: 22 },
});
function Card({ title, body }: { title: string; body: string }) {
return (
<View style={styles.card}>
<Text style={styles.title}>{title}</Text>
<Text style={styles.body}>{body}</Text>
</View>
);
}
// Style composition - array of styles (later overrides earlier)
<View style={[styles.card, isActive && styles.cardActive, { marginBottom: 8 }]} />
// Design token system - centralize colors and spacing
// src/constants/theme.ts
export const COLORS = {
primary: '#229c6b',
surface: '#1e293b',
text: { primary: '#f1f5f9', secondary: '#94a3b8' },
border: '#334155',
} as const;
export const SPACING = { xs: 4, sm: 8, md: 16, lg: 24, xl: 32 } as const;
export const RADIUS = { sm: 6, md: 10, lg: 16, full: 9999 } as const;Shadows - iOS vs Android
// iOS - shadow properties
const iosCard = {
shadowColor: '#000',
shadowOffset: { width: 0, height: 4 },
shadowOpacity: 0.12,
shadowRadius: 8,
};
// Android - elevation (Material Design shadow)
const androidCard = { elevation: 4 };
// Cross-platform shadow utility
import { Platform } from 'react-native';
export const shadow = (level: 1 | 2 | 3 | 4) =>
Platform.select({
ios: {
shadowColor: '#000',
shadowOffset: { width: 0, height: level * 1.5 },
shadowOpacity: 0.1 + level * 0.02,
shadowRadius: level * 2,
},
android: { elevation: level * 2 },
});
// Usage
<View style={[styles.card, shadow(2)]} />Tip
Tip
Practice StyleSheet API Performance and Patterns in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
StyleSheet.create optimizations, pixel density, and dynamic styles
Practice Task
Note
Practice Task - (1) Write a working example of StyleSheet API Performance and Patterns from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with StyleSheet API Performance and Patterns is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react native code.
Finished this lesson?