Styling Implementation Guide
Guidelines for implementing styles using StyleSheet and Unistyles.
Styling Implementation Guide
Overview
This guide details the patterns and best practices for styling components in our React Native application. We primarily use React Native StyleSheet for core styling and Unistyles for advanced features like theming, responsive styles, and performance optimization.
When To Use
StyleSheet.create: For standard, static component styles.Unistyles(createStyleSheet,useStyles):- When styles need to adapt based on theme (light/dark mode).
- For responsive styles that change based on screen size or orientation.
- When accessing theme variables (colors, spacing, fonts) directly within styles.
- For performance-critical components where Unistyles' optimizations are beneficial.
Implementation Patterns
1. Basic Styling with StyleSheet.create
Use StyleSheet.create for simple, non-dynamic styles. This is the most performant method for static styles.
2. Theming and Dynamic Styles with Unistyles
Define themes and breakpoints, then create stylesheets that adapt.
a) Define Themes and Breakpoints:
b) Register Unistyles:
c) Create a Unistyles Stylesheet:
d) Use the Stylesheet in a Component:
3. Combining StyleSheet and Unistyles
You can mix both approaches. Use StyleSheet for static parts and useStyles for theme-dependent parts.
4. Responsive Styles (Breakpoints)
Unistyles allows defining styles specific to breakpoints.
5. Global Styles and Style Utilities
Define reusable style patterns and utilities in the core/styles directory.
Common Challenges
- Over-reliance on Inline Styles: While convenient for quick adjustments, excessive inline styles (
style={{ color: 'red' }}) hurt performance and maintainability. PreferStyleSheet.createor Unistyles. - Performance with Dynamic Styles: Creating new style objects in the render function (
style={{ backgroundColor: props.isActive ? 'blue' : 'grey' }}) can cause unnecessary re-renders. UseStyleSheet.flattenwith conditional styles fromStyleSheet.createor leverage Unistyles variants/dynamic functions. - Specificity and Overrides: Understanding how styles cascade and override each other is important. Styles passed via the
styleprop typically override styles defined within the component. - Platform Differences: Some style properties behave differently or are unavailable on iOS vs. Android (e.g.,
elevationvs.shadow*). Test on both platforms or use platform-specific code (Platform.select) when necessary.
Performance Considerations
- (Do ✅) Use
StyleSheet.createfor static styles. React Native optimizes these by sending them over the bridge only once. - (Do ✅) Use
Unistylesfor themed and responsive styles. It's highly optimized for dynamic style generation. - (Don't ❌) Use Inline Styles (
style={{...}}) frequently, especially within loops or frequently re-rendering components, as it creates new objects on every render. - (Don't ❌) Create Style Objects in Render: Avoid defining style objects directly within the component function body. Define them outside using
StyleSheet.createorcreateStyleSheetfrom Unistyles. - (Consider 🤔)
StyleSheet.flatten: UseStyleSheet.flatten([styles.base, conditionalStyle])when conditionally applying styles fromStyleSheet.create, but be aware it has a minor performance cost compared to purely static styles.
Examples
Themed Button using Unistyles
Related Documents
- DOC-01: Core Architecture Reference
- DOC-02: Project Structure Reference