Components and text

Write a component, show text, and group things in a View.

Your React Native code runs inside your browser tab on React Native for Web, shown on a phone-sized screen. Nothing to install.

Your first screen

A React Native app is made of components. A component is a function that returns what should be on screen, written in tags much like HTML.

<Text> shows words. All text in React Native must be inside a <Text>. <View> is a container: a box that holds other components.

The line starting import brings in the components you use, and export default marks the component to show. Load the example and press Run.

Example
import { Text, View } from "react-native";

export default function App() {
  return (
    <View>
      <Text>Hello, Asha</Text>
    </View>
  );
}

Your turn: show some text

The editor starts with the lines every screen needs.

Practice task

Show the words Good morning on the screen.

Your answer is run and checked in the lesson. Try it now.

Several things in a View

A component returns one thing. To show several, wrap them in a <View>.

Inside a View, components are stacked from top to bottom, in the order you write them.

Example
import { Text, View } from "react-native";

export default function App() {
  return (
    <View>
      <Text>Today's menu</Text>
      <Text>Masala dosa</Text>
      <Text>Filter coffee</Text>
    </View>
  );
}

Your turn: two lines

Each line is its own Text.

Practice task

Inside the View, show two separate lines of text: Tea, and under it Coffee.

Your answer is run and checked in the lesson. Try it now.

Showing a value

Inside the tags, curly brackets switch back to JavaScript. Whatever value is between them is shown.

This is how a screen shows something that is worked out, rather than typed in.

Example
import { Text, View } from "react-native";

export default function App() {
  const name = "Asha";
  const messages = 3;
  return (
    <View>
      <Text>Hello, {name}</Text>
      <Text>You have {messages} new messages</Text>
    </View>
  );
}

Your turn: show a number

Use curly brackets inside the Text.

Practice task

Above the return, create a constant called price holding 40. Show the words Price: 40, taking the number from the constant.

Your answer is run and checked in the lesson. Try it now.