Rangle.io: React Native Workshop
gitbook
gitbook
  • Introduction
  • Setup
    • Hello World
    • Debugging
  • Core Concepts
    • Components
    • Styles
    • Flexbox
    • APIs
  • Navigation
    • Navigation Experimental
    • Creating Some Helpers
    • The Navigator Component
  • ListView (Pokédex)
    • List View
    • List View: Render Row
    • Text Input
    • Keyboard Spacer
  • Selectors (Filtering)
  • Animation
    • LayoutAnimation
    • Animated
      • Animated.Value
      • Timing, Spring & Decay
      • Animated Components
      • More Animated
    • Resources
    • Exercise
  • ScrollView (Pokemon Details)
  • MapView
  • Testing
  • Gesture Responder System
    • PanResponder
Powered by GitBook
On this page
  • Usage for Static Styles
  • Usage for Dynamic Styles

Was this helpful?

  1. Core Concepts

Styles

PreviousComponentsNextFlexbox

Last updated 6 years ago

Was this helpful?

React Native allows you to style components using a subset of CSS properties as inline styles. For layout only the flexbox module and absolute positioning is available. The are split into five categories:

  1. View Properties

  2. Image Properties

  3. Text Properties

  4. Flex Properties

  5. Transform Properties

Usage for Static Styles

Use StyleSheet.create to construct styles and define them at the end of the file. This ensures that the values are immutable and they are only created once for the application and not on every render.

var RedBox = ({ children }) => {
  return (
    <View style={ styles.base }>
      { children }
    </View>
  );
};

const styles = StyleSheet.create({
  base: {
    backgroundColor: '#222222',
    color: '#fff',
  },
  active: {
    backgroundColor: '#85144b',
    color: '#B10DC9'
  },
});

You can also compose styles

// As an array
<View style={[styles.base, styles.background]} />
// or conditionally
<View style={[styles.base, this.state.active && styles.active]} />

Usage for Dynamic Styles

Dynamic styles can be created as objects in the render. However, the official documentation recommends that you avoid this:

Finally, if you really have to, you can also create style objects in render, but they are highly discouraged. Put them last in the array definition.

var RedBox = ({ children, width }) => {
  return (
    <View style={[styles.base, {
      width: this.state.width,
    }]}>
      { children }
    </View>
  );
};

const styles = StyleSheet.create({
  base: {
    backgroundColor: '#222222',
    color: '#fff',
  },
  active: {
    backgroundColor: '#85144b',
    color: '#B10DC9'
  },
});
style properties