React Native works like the React you know, but its building blocks are components backed by real Android and iOS views, where the web uses DOM elements. Tags such as div, p and span don't exist here. You build screens from React components instead, starting with the Core Components that ship with React Native.

Key terms

  • Native view: in Android and iOS development, the basic building block of the UI, a small rectangle on the screen that displays text or images, or responds to user input.
  • Native Components: React components that are backed by the platform's own views on Android and iOS.
  • Core Components: the ready-to-use Native Components that ship with React Native, such as View, Text and Image.

Core components become native views

You describe the UI with React components in JavaScript, and at runtime React Native creates the corresponding Android and iOS views for the Native Components you render. View, for example, maps directly to the native view of whichever platform the app runs on. A View that only lays out its children and draws nothing may be left out of the native hierarchy as an optimization, and setting its collapsable prop to false keeps it in.

Beyond the Core Components, React Native lets you build your own Native Components for Android and iOS, and the community publishes many more.

Porting a like button from the web

Here is a like button written with React for the web:

tsx
import {useState} from 'react';

export default function LikeButton() {
  const [liked, setLiked] = useState(false);

  return (
    <div className="like">
      <p>{liked ? 'You liked this post' : 'Not liked yet'}</p>
      <button onClick={() => setLiked(!liked)}>{liked ? 'Unlike' : 'Like'}</button>
    </div>
  );
}

The LikeButton example below the lesson is its React Native version. The port keeps the useState call and swaps the tags: View takes the place of the div, Text of the p, and Button of the button. className has to go, because no React Native component has a className prop. Styles go through the style prop instead, which View and Text accept, and the Styling and Flexbox Layout section covers it. Button is an exception, with no style prop at all. It takes a color prop, which sets the text color on iOS and the background color on Android.

Props, state and hooks carry over unchanged. A function component behaves the same as on the web, and React Native handles state no differently from React, in class components as well as in function components with hooks.

React's own hooks such as useState keep coming from react, and the components come from react-native, as the imports at the top of the LikeButton example show:

tsx
import {useState} from 'react';
import {Button, Text, View} from 'react-native';

Neither line imports React itself, and JSX doesn't need it: the App.tsx in React Native's app template renders JSX without importing anything from react.

The label moves as well. The web button took it as children, but Button needs it as a title string, so the ternary goes into that prop, and onPress takes the place of onClick:

tsx
<Button title={liked ? 'Unlike' : 'Like'} onPress={() => setLiked(!liked)} />

On Android, the title is shown uppercased.

View, Text, Image, ScrollView and TextInput

React Native has many core components, but these five are the ones you will reach for most. Each one is a named import from react-native:

ComponentWhat it doesClosest web element
ViewA container that doesn't scroll and supports Flexbox layout, style, some touch handling and accessibility controlsa div that doesn't scroll
TextDisplays, styles and nests strings of text, and can handle touch eventsp
ImageDisplays images from the network, static resources, temporary local images and images on disk such as the camera rollimg
ScrollViewA generic scrolling container that can hold several components and viewsdiv
TextInputLets the user enter text with the keyboardinput type="text"

The two containers differ in whether their content scrolls. A ScrollView can hold children of different kinds, and it works best for a small number of things of limited size, such as a photo and a few paragraphs. A long list that can't fit on the screen belongs in a FlatList, which the last section of this course covers.

Image gets the picture from its source prop, which takes either a remote URL or a local file resource. TextInput is the basic component for typing text into the app, and the Text Input and Touch section shows how to read what the user types.

How BookScreen nests them

BookScreen uses all five. A ScrollView wraps the whole screen so it can scroll when a long summary runs past the bottom, and an inner View groups the cover with the title. The cover is a network image, and for a network image you have to set the width and height yourself:

tsx
<Image source={{uri: book.coverUrl}} style={{width: 120, height: 180}} />

The <TextInput /> at the end has no props yet. They get their own lessons later in the course.

Code Examples

tsx
import {Image, ScrollView, Text, TextInput, View} from 'react-native';

type Book = {
  title: string;
  summary: string;
  coverUrl: string;
};

export default function BookScreen({book}: {book: Book}) {
  return (
    <ScrollView>
      <View>
        <Image source={{uri: book.coverUrl}} style={{width: 120, height: 180}} />
        <Text>{book.title}</Text>
      </View>
      <Text>{book.summary}</Text>
      <TextInput />
    </ScrollView>
  );
}
tsx
import {useState} from 'react';
import {Button, Text, View} from 'react-native';

export default function LikeButton() {
  const [liked, setLiked] = useState(false);

  return (
    <View>
      <Text>{liked ? 'You liked this post' : 'Not liked yet'}</Text>
      <Button title={liked ? 'Unlike' : 'Like'} onPress={() => setLiked(!liked)} />
    </View>
  );
}
✓ Completed