tezvyn:

The View Component: React Native's UI Building Block

AI-drafted, machine-checkedSource: reactnative.devbeginner

Think of `View` as the `<div>` of React Native. It’s the fundamental container for grouping and styling other components, used everywhere to structure layouts with flexbox. The footgun: Text must be in a `<Text>` component, not directly in a `View`.

WHY IT EXISTS: To provide a basic, cross-platform building block for user interfaces. React Native needs a universal container that can translate into the specific native UI view for each platform (iOS, Android, etc.), ensuring consistent layout and styling behavior from a single codebase.

THE MENTAL MODEL: The View component is the of React Native. It's a generic, non-visible box used to group, style, and structure other components. It doesn't have its own appearance but serves as a container to which you apply styles like flexbox, padding, and background colors.

HOW IT WORKS: A View component maps directly to the underlying native view of the host platform, such as UIView on iOS or android.view on Android. It's designed to be nested, allowing you to build complex layouts by composing Views inside other Views. You control its layout and appearance primarily through the style prop, often using flexbox to arrange its children.

WHEN TO USE IT: Use View whenever you need to create a container for other components. This is its primary purpose. Use it to group elements you want to style together, to create rows or columns using flexDirection, or to add padding and margins around a section of your UI. It's the most common component for layout.

WHEN NOT TO USE IT: Do not use View to display text directly. All text content must be placed inside a <Text> component, or your app will throw an error. For long, scrollable lists of items, use more specialized components like FlatList or ScrollView for better performance and memory management instead of a standard View.

ONE CANONICAL EXAMPLE: A common pattern is creating a card layout. You would have an outer View with a background color and padding. Inside, you might have another View using flexDirection: 'row' to place an Image and a Text component side-by-side. This demonstrates grouping, styling, and layout with nested Views.

Read the original → reactnative.dev

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.