Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Styling | Basic Concepts
Foundations of React Native

StylingStyling

Before we proceed with working on components and delving deeper into React Native, let's address styling. Currently, we have added three elements to our project, but the visual presentation may need to be more optimal.

content
In React Native, styling is done using both inline styles and the StyleSheet API. Here's an overview of each along with real-world examples:

Inline Styles

Inline styles in React Native are similar to how you write styles in regular React for the web. Styles are passed as objects directly within the component's props.

Theory

  • Inline styles allow for a quick and straightforward way to apply styles directly to a component;
  • Inline styles use JavaScript objects to define styles directly within the component's props;
  • Each style property is written in camelCase, similar to how it's done in CSS, but without hyphens;
  • Values can be either strings or numbers, depending on the property.

Example

Code Description

View Styles:

  • Line 8: flex: 1: The View component takes up all available space within its container, ensuring it expands to fill the entire screen.
  • Line 9: justifyContent: "center": Vertically centers the content within the View.
  • Line 10: alignItems: "center": Horizontally centers the content within the View.
  • Line 11: backgroundColor: "lightblue": Sets the background color of the View to light blue.
  • Text Styles:

  • Line 14: fontSize: 24: Sets the font size of the Text component to 24 units.
  • Line 14: color: "firebrick": Sets the text color of the Text component to firebrick.
  • StyleSheet

    React Native encourages the use of the StyleSheet API for defining styles, especially for performance optimization.

    Theory

    • The StyleSheet.create method is used to create an optimized style object;
    • Styles created with StyleSheet.create are processed during build time, resulting in better performance compared to inline styles;
    • Style properties are defined in the same camelCase format as inline styles;
    • The resulting object is then referenced in the component.

    Example

    Code Description
    • Line 2: Import StyleSheet from react-native.
    • Lines 6-7: Apply the class names container and text to the appropriate elements.
    • Lines 14-25: Create a stylesheet object using the StyleSheet.create method. This object contains style definitions for the container and text styles.
    • All styles remain the same as in the previous example.

    Result after implementing any of the approaches.

    In Practice

    Everything was clear?

    Section 2. Chapter 5
    course content

    Course Content

    Foundations of React Native

    StylingStyling

    Before we proceed with working on components and delving deeper into React Native, let's address styling. Currently, we have added three elements to our project, but the visual presentation may need to be more optimal.

    content
    In React Native, styling is done using both inline styles and the StyleSheet API. Here's an overview of each along with real-world examples:

    Inline Styles

    Inline styles in React Native are similar to how you write styles in regular React for the web. Styles are passed as objects directly within the component's props.

    Theory

    • Inline styles allow for a quick and straightforward way to apply styles directly to a component;
    • Inline styles use JavaScript objects to define styles directly within the component's props;
    • Each style property is written in camelCase, similar to how it's done in CSS, but without hyphens;
    • Values can be either strings or numbers, depending on the property.

    Example

    Code Description

    View Styles:

  • Line 8: flex: 1: The View component takes up all available space within its container, ensuring it expands to fill the entire screen.
  • Line 9: justifyContent: "center": Vertically centers the content within the View.
  • Line 10: alignItems: "center": Horizontally centers the content within the View.
  • Line 11: backgroundColor: "lightblue": Sets the background color of the View to light blue.
  • Text Styles:

  • Line 14: fontSize: 24: Sets the font size of the Text component to 24 units.
  • Line 14: color: "firebrick": Sets the text color of the Text component to firebrick.
  • StyleSheet

    React Native encourages the use of the StyleSheet API for defining styles, especially for performance optimization.

    Theory

    • The StyleSheet.create method is used to create an optimized style object;
    • Styles created with StyleSheet.create are processed during build time, resulting in better performance compared to inline styles;
    • Style properties are defined in the same camelCase format as inline styles;
    • The resulting object is then referenced in the component.

    Example

    Code Description
    • Line 2: Import StyleSheet from react-native.
    • Lines 6-7: Apply the class names container and text to the appropriate elements.
    • Lines 14-25: Create a stylesheet object using the StyleSheet.create method. This object contains style definitions for the container and text styles.
    • All styles remain the same as in the previous example.

    Result after implementing any of the approaches.

    In Practice

    Everything was clear?

    Section 2. Chapter 5
    some-alt