Skip to main content

Understanding React Native Components and JSX

Your goal: Build a Reading List screen and explain how reusable components describe its interface.

Before you begin: Complete Development Environment. Keep your learning project open and run npx expo start from its folder. You should recognize functions, imports, and strings; we will explain the small amount of TypeScript used here.

What is a component?​

A component is a reusable building block of an interface. Components are typically JavaScript or TypeScript functions that return a UI description using JSX, the tag-like syntax you saw in the first lesson.

Here is a small example to read:

A small component
import { Text } from 'react-native';

function ReadingListTitle() {
return <Text>My Reading List</Text>;
}

ReadingListTitle describes one piece of the screen. Another component can include it by writing <ReadingListTitle />. Start custom component names with a capital letter so React recognizes them as components.

Rendering means React calls a component to work out the interface it describes. A re-render repeats that calculation with the current inputs and state; it does not necessarily change anything visible. See React’s render and commit explanation.

Components can contain other components. A screen might contain a heading and several book cards; each card can contain text. Giving those pieces names makes a screen easier to organize. Reusing a card also means you can change its markup in one place. See React Native’s component introduction.

What is JSX?​

JSX lets you describe UI with tags inside JavaScript or TypeScript. Your build tooling transforms that syntax into JavaScript the app can run. TypeScript files containing JSX use the .tsx extension.

Although JSX resembles HTML, native React Native screens do not use browser DOM elements such as div or p. They use components such as View, Text, and Image to describe native UI.

Curly braces let you insert a JavaScript expression—a value or calculation—into JSX:

A value inside JSX
import { Text } from 'react-native';

function ReadingListTitle() {
const heading = 'My Reading List';
return <Text>{heading}</Text>;
}

{heading} displays the variable’s value: My Reading List. Writing <Text>heading</Text> would display the literal word heading instead. Quotes supply text values; braces let JSX use JavaScript values. You will also see braces in attributes, such as style={styles.screen}.

Close every tag: use <Text>Words</Text> when it contains content, or a self-closing tag such as <ReadingListTitle /> when it does not. A component’s returned JSX needs a single enclosing element; our screens use a View. React’s JSX guide explains these syntax rules, although its examples use web elements.

Checkpoint: What would <Text>{2 + 1} books</Text> display? You should expect 3 books.

Meet three core components​

View groups content​

A View is a container for other components. This fragment groups a title and author:

<View>
<Text>The Hobbit</Text>
<Text>J. R. R. Tolkien</Text>
</View>

Text displays words​

Use Text for visible strings and numbers. Even a short label belongs inside it:

<Text>Books I want to read</Text>

These two fragments assume you have imported View and Text from react-native, as the complete screen below does.

Image displays a picture​

Image can show a bundled asset or a remote picture. This optional, standalone example uses a public React Native logo so you do not need to add an asset file:

Optional image example
import { Image } from 'react-native';

function ReadingLogo() {
return (
<Image
source={{ uri: 'https://reactnative.dev/img/tiny_logo.png' }}
style={{ width: 50, height: 50 }}
accessible={true}
accessibilityLabel="React Native logo"
/>
);
}

source tells the image where to load from. Remote images need explicit dimensions and an internet connection. The outer braces in source={{ uri: '...' }} enter JavaScript; the inner braces create an object containing the image address. The same pattern supplies a style object. The accessibility label describes this informative image to a screen reader.

Read this example separately; the Reading List below needs only text and containers, and loads no remote data.

Build the Reading List screen​

In the screen file you edited in the previous lesson, replace its contents with the following code. That lesson uses src/app/index.tsx; if your template differs, use the file where you successfully changed the welcome greeting. Keep the surrounding project and its layout files in place. In a simple project with an App.tsx entry file, you can paste it there instead.

Your starting screen file
import { StyleSheet, Text, View } from 'react-native';

export default function App() {
return <ReadingListScreen />;
}

function ReadingListScreen() {
return (
<View style={styles.screen}>
<Text style={styles.heading}>My Reading List</Text>
<Text style={styles.subtitle}>Three books to explore next</Text>
<View style={styles.card}>
<Text style={styles.bookTitle}>The Hobbit</Text>
<Text>J. R. R. Tolkien</Text>
</View>
<View style={styles.card}>
<Text style={styles.bookTitle}>A Wizard of Earthsea</Text>
<Text>Ursula K. Le Guin</Text>
</View>
<View style={styles.card}>
<Text style={styles.bookTitle}>The Secret Garden</Text>
<Text>Frances Hodgson Burnett</Text>
</View>
</View>
);
}

const styles = StyleSheet.create({
screen: { padding: 24, paddingTop: 64 },
heading: { fontSize: 28, fontWeight: '700' },
subtitle: { marginBottom: 20 },
card: { padding: 12, marginBottom: 12, borderWidth: 1 },
bookTitle: { fontSize: 18, fontWeight: '600' },
});

Save and look at your phone. You should see a title, a subtitle, and three bordered book entries, each with a title above an author.

The outer View contains two Text components and three inner View components. Each inner container groups the two text elements for one book. JSX indentation helps you see which elements belong together; the tags define that relationship.

StyleSheet.create gives names to our style objects. style={styles.card} applies the card style. Padding adds space inside a container; margin adds space outside it. Font size and weight distinguish headings, and a border outlines each entry. We will explore styling and layout in a later lesson.

A small learning screen

The top padding provides simple practice spacing; it is not device-aware safe-area handling. This fixed screen also does not scroll. Keep the example short for now; larger lists and different device insets need layout work beyond this lesson.

Checkpoint: Find the three repeated inner containers. What differs between them? Only the book title and author.

Extract a BookCard​

Repeated markup is a useful signal that a component could help. We will move one book’s container into BookCard, then supply different values each time we use it.

Those values are called props: properties passed from a parent component to a child. For now, our card receives just title and author:

The reusable piece
import { Text, View } from 'react-native';

type BookCardProps = {
title: string;
author: string;
};

function BookCard({ title, author }: BookCardProps) {
return (
<View>
<Text>{title}</Text>
<Text>{author}</Text>
</View>
);
}

BookCardProps describes the expected values for TypeScript: both are strings. The { title, author } parameter takes those two named values from the props object. The braces inside each Text insert those values into the UI.

A parent supplies them using JSX attributes:

<BookCard title="The Hobbit" author="J. R. R. Tolkien" />

This renders one card. Three uses render three cards with their own supplied values. Props get a fuller treatment in Props and State; this is enough to make our repeated markup reusable.

Run the complete example​

Replace the same screen file with this version. It includes all imports, types, components, and styles; you do not need to combine the earlier fragments.

Your starting screen file — complete version
import { StyleSheet, Text, View } from 'react-native';

type BookCardProps = {
title: string;
author: string;
};

function BookCard({ title, author }: BookCardProps) {
return (
<View style={styles.card}>
<Text style={styles.bookTitle}>{title}</Text>
<Text>{author}</Text>
</View>
);
}

function ReadingListScreen() {
return (
<View style={styles.screen}>
<Text style={styles.heading}>My Reading List</Text>
<Text style={styles.subtitle}>Three books to explore next</Text>
<BookCard title="The Hobbit" author="J. R. R. Tolkien" />
<BookCard title="A Wizard of Earthsea" author="Ursula K. Le Guin" />
<BookCard title="The Secret Garden" author="Frances Hodgson Burnett" />
</View>
);
}

export default function App() {
return <ReadingListScreen />;
}

const styles = StyleSheet.create({
screen: { padding: 24, paddingTop: 64 },
heading: { fontSize: 28, fontWeight: '700' },
subtitle: { marginBottom: 20 },
card: { padding: 12, marginBottom: 12, borderWidth: 1 },
bookTitle: { fontSize: 18, fontWeight: '600' },
});

The screen should look the same after saving. Its organization has changed: one BookCard function now describes all three entries. Everything stays in one file so you can follow the relationships. The default export lets your existing app entry or Expo Router screen load this example; naming that function App does not add a new route.

Checkpoint: Change only the first card’s title attribute. Only that book’s title should change. Changing the markup inside BookCard, however, affects every card that uses it.

Follow the component hierarchy​

A parent includes another component in its JSX; the included component is its child. App is the parent of ReadingListScreen, and the screen is the parent of the three BookCard instances. The cards are siblings: they share a parent.

This diagram focuses on custom components and leaves out their built-in View and Text elements. There is one BookCard definition, used three times. Keeping reusable pieces together this way is called composition.

Fix common mistakes​

If you see…Check this
An error that View or Text cannot be foundAdd import { View, Text } from 'react-native'; to that file. Include StyleSheet too if the file uses it.
An error about text strings outside a text componentWrite <View><Text>Hello</Text></View>. A raw string in <View>Hello</View> is not valid native text content.
An error after using div or pReplace browser elements with native components such as View and Text, and import them.
A custom component is not recognizedUse a capitalized name in both the definition and JSX: function BookCard(...) and <BookCard ... />.
A syntax error near a closing tagMatch opening and closing tags, and close children before their parent. Use <BookCard ... /> for a card with no nested content.

For example, <View><Text>Hello</View> is missing </Text>. The corrected JSX is:

<View>
<Text>Hello</Text>
</View>

Practice​

Try these changes to the complete example without copying a solution:

  1. Add a fourth book. Reuse BookCard with a new title and author. Update the subtitle too. Check that each entry displays the intended pair of values.
  2. Create SectionHeading. Move the screen’s heading markup into a component and render it in the same position. Check that the heading still appears once with the same style.
  3. Add a category label. Put a Text element reading “Fiction” inside BookCard. Check that it appears in every card after a single edit to the component definition.

Check your understanding​

  1. What does a component function return?
  2. How does <Text>title</Text> differ from <Text>{title}</Text>?
  3. Which component supplies each card’s title and author?
  4. Why are there three cards when there is only one BookCard function?
Compare your answers

Our component functions return JSX describing the UI. The first text example displays the word “title”; the second displays the variable’s value. ReadingListScreen supplies each card’s props. Each use of <BookCard /> describes a separate card using the same component definition.

You are ready to continue when: You can add a book, identify its parent component, and explain how the card receives its text.

Next: Understanding Props and State in React Native. Make each book card interactive, or return to the learning path.