React Native course ยท Module 1: Your first app

Basic React Native Components

3 min read ยท
In this lesson5

In React Native, we don't use HTML - instead we have native components. Let's learn the most important ones!

View - Container (equivalent of div)

View is the basic container, like <div> in HTML:

import { View, StyleSheet } from 'react-native';

export default function ContainerExample() {
  return (
    <View style={styles.outer}>
      <View style={styles.box1} />
      <View style={styles.box2} />
      <View style={styles.box3} />
    </View>
  );
}

const styles = StyleSheet.create({
  outer: {
    flex: 1,
    flexDirection: 'row',
    justifyContent: 'space-around',
    alignItems: 'center',
    backgroundColor: '#1a5276',
  },
  box1: {
    width: 80,
    height: 80,
    backgroundColor: '#3498db',
    borderRadius: 10,
  },
  box2: {
    width: 80,
    height: 80,
    backgroundColor: '#2ecc71',
    borderRadius: 40, // Circle
  },
  box3: {
    width: 80,
    height: 80,
    backgroundColor: '#e74c3c',
    borderRadius: 10,
  },
});

Text - Displaying Text

In React Native all text must be inside a Text component:

import { View, Text, StyleSheet } from 'react-native';

export default function TextExample() {
  const userName = "Traveler";

  return (
    <View style={styles.container}>
      {/* Basic text */}
      <Text style={styles.heading}>Welcome to Atlantis!</Text>

      {/* Text with interpolation */}
      <Text style={styles.greeting}>Hello, {userName}!</Text>

      {/* Nested text (inline styles) */}
      <Text style={styles.paragraph}>
        Atlantis is a <Text style={styles.bold}>sunken city</Text> full of{' '}
        <Text style={styles.italic}>mysteries</Text> and{' '}
        <Text style={styles.highlight}>treasures</Text>.
      </Text>

      {/* Multiline text */}
      <Text style={styles.multiline} numberOfLines={3}>
        This text will be truncated after three lines if it is too long.
        Lorem ipsum dolor sit amet, consectetur adipiscing elit.
        Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
        Ut enim ad minim veniam, quis nostrud exercitation.
      </Text>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    padding: 20,
    backgroundColor: '#1a5276',
  },
  heading: {
    fontSize: 24,
    fontWeight: 'bold',
    color: '#fff',
    marginBottom: 16,
    textAlign: 'center',
  },
  greeting: {
    fontSize: 18,
    color: '#aed6f1',
    marginBottom: 16,
  },
  paragraph: {
    fontSize: 16,
    color: '#fff',
    lineHeight: 24,
    marginBottom: 16,
  },
  bold: {
    fontWeight: 'bold',
  },
  italic: {
    fontStyle: 'italic',
  },
  highlight: {
    color: '#f1c40f',
    fontWeight: 'bold',
  },
  multiline: {
    fontSize: 14,
    color: '#bdc3c7',
    lineHeight: 20,
  },
});

Image - Displaying Images

import { View, Image, StyleSheet } from 'react-native';

export default function ImageExample() {
  return (
    <View style={styles.container}>
      {/* Local image */}
      <Image
        source={require('../assets/images/atlantis-logo.png')}
        style={styles.localImage}
      />

      {/* Image from URL */}
      <Image
        source={{ uri: 'https://picsum.photos/200/200' }}
        style={styles.remoteImage}
      />

      {/* Image with different scaling modes */}
      <Image
        source={{ uri: 'https://picsum.photos/400/200' }}
        style={styles.coverImage}
        resizeMode="cover" // cover, contain, stretch, center
      />
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    padding: 20,
    backgroundColor: '#1a5276',
  },
  localImage: {
    width: 100,
    height: 100,
    marginBottom: 20,
  },
  remoteImage: {
    width: 150,
    height: 150,
    borderRadius: 75, // Round image
    marginBottom: 20,
  },
  coverImage: {
    width: '100%',
    height: 200,
    borderRadius: 10,
  },
});

Differences Between React (Web) and React Native

React (Web)React NativeDescription
<div><View>Container
<span>, <p>, <h1><Text>Text
<img><Image>Images
<input><TextInput>Text fields
<button><Pressable>, <TouchableOpacity>Buttons
<ul>, <li><FlatList>Lists
<a><Link> (expo-router)Links
CSS classesStyleSheetStyles
onClickonPressClick events

Important Rules

  1. Text MUST be inside <Text> - otherwise error!

    // Error!
    <View>Hello</View>
    
    // Correct
    <View><Text>Hello</Text></View>
  2. Styles as JavaScript objects

    // No CSS classes
    <View className="container">
    
    // StyleSheet or inline
    <View style={styles.container}>
    <View style={{ flex: 1 }}>
  3. Flexbox defaults to column

    // Web: flex-direction: row (default)
    // React Native: flexDirection: 'column' (default)
Code for this lesson: App.tsx
import React from 'react';
import { View, Text, StyleSheet } from 'react-native';

export default function App() {
  return (
    <View style={styles.container}>
      <Text style={styles.text}>Welcome to Atlantis!</Text>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    // TODO: set flex: 1 so the container fills the whole screen
    // TODO: center the content vertically (justifyContent: 'center')
    // TODO: center the content horizontally (alignItems: 'center')
    backgroundColor: '#1a5276',
  },
  text: {
    fontSize: 22,
    color: '#fff',
  },
});

Docs entries used in this lesson

Spotted a mistake in this lesson?

Check yourself

Answer the questions from this lesson. Pick an answer to see right away whether it is correct.

  1. 1. Which component in React Native is the equivalent of <div> from HTML?

  2. 2. Why is this code incorrect: <View>Hello World</View>?

Hands-on tasks in the game

  • Click in order

    Click the elements in the correct order to create a View component:

  • Horizontal ordering

    What is the equivalent of <img> in React Native?

  1. QuestionQuiz: The equivalent of div in React Native
  2. Click in orderSort: Building a View component
  3. QuestionQuiz: Displaying text
  4. Put in orderMatch: HTML vs React Native

Useful articles