StyleSheet API provides an abstraction similar to CSS StyleSheets for creating and managing component styles. It offers performance benefits through style validation and optimization.
Import
Methods
create()
Creates an optimized StyleSheet from a style object.object
required
An object containing named style definitions.Each key becomes a style name, and each value is a style object.
- Validates style properties at creation time
- Freezes style objects in development mode
- Enables performance optimizations
- Provides better error messages
- Allows style IDs for efficient bridging
flatten()
Flattens an array of style objects into a single aggregated style object.array | object
required
A style object, array of styles, or nested array of styles.
compose()
Combines two styles, with the second style overriding properties in the first.object
First style object (base styles).
object
Second style object (override styles).
Properties
hairlineWidth
The thinnest line width that can be displayed on the device.number - Typically 1 / PixelRatio.get(), always at least 1 physical pixel.
absoluteFill
Pre-defined style for absolute positioning that fills the entire parent.Examples
Basic Usage
Combining Styles
Using compose()
Using flatten()
Using hairlineWidth
Using absoluteFill
Responsive Styles
Conditional Styles
Shadows and Elevation
Reusable Style Compositions
Dynamic Styles
Nested Style Arrays
Style Properties
Layout Props
width,heightmargin,marginTop,marginRight,marginBottom,marginLeftpadding,paddingTop,paddingRight,paddingBottom,paddingLeftflex,flexDirection,flexWrap,flexGrow,flexShrink,flexBasisjustifyContent,alignItems,alignSelf,alignContentposition('relative','absolute')top,right,bottom,leftzIndex
Transform Props
Color and Background
backgroundColoropacityoverflow('visible','hidden','scroll')
Border Props
borderWidth,borderTopWidth,borderRightWidth,borderBottomWidth,borderLeftWidthborderColor,borderTopColor,borderRightColor,borderBottomColor,borderLeftColorborderRadius,borderTopLeftRadius,borderTopRightRadius,borderBottomLeftRadius,borderBottomRightRadiusborderStyle('solid','dotted','dashed')
Shadow Props (iOS)
shadowColorshadowOffset({ width: number, height: number })shadowOpacityshadowRadius
Elevation (Android)
elevation
Text Props
colorfontSize,fontWeight,fontFamily,fontStylelineHeight,letterSpacingtextAlign('left','right','center','justify')textDecorationLine('none','underline','line-through')textTransform('none','uppercase','lowercase','capitalize')
Performance Tips
Use
StyleSheet.compose() for optimal performance when combining two styles, especially in reusable components.