At the heart of every React Native application are a handful of core components that act as the foundational building blocks for your user interface. Think of View as the equivalent of a div in web development – it's your primary container for layout, grouping other components, and applying styles. It doesn't display anything itself but is crucial for structuring your UI. Text is your go-to for displaying any text; unlike web, all textual content in React Native must be wrapped within a <Text> component. This allows the framework to apply native text rendering and styling efficiently. For visual content, the Image component handles displaying pictures, whether they are local assets bundled with your app or fetched from a network URL. Mastering these three components gives you the basic tools to render static content on the screen.
As your content grows, you'll inevitably need to handle scenarios where information exceeds the screen's available space. This is where ScrollView and FlatList come into play. ScrollView is perfect for finite, relatively short amounts of content that might scroll, like a settings page or a blog post. It renders all its children at once, making it straightforward to use, but potentially inefficient for very long lists. For dynamic, potentially infinite, and truly long lists of data, FlatList is the hero. It's built for performance, efficiently rendering only the items currently visible on screen (a process called virtualization), which significantly reduces memory usage and improves rendering speed for large datasets. You provide FlatList with your data and a function to render each item, and it handles the rest.
Understanding View, Text, Image, ScrollView, and FlatList is non-negotiable for any React Native developer. They are the primitives you'll combine and style using the style prop (often with StyleSheet.create) to construct virtually any mobile UI. While they may seem simple individually, their effective combination and understanding of their nuances, especially the performance implications between ScrollView and FlatList, are key to building robust, performant, and user-friendly cross-platform applications.
Key Takeaways
- View is the fundamental container for layout and styling, analogous to a web
div. - All text in React Native must be wrapped within a
Textcomponent. - Use
Imageto display pictures from local assets or network URLs. ScrollViewis for finite, short lists; it renders all content at once.FlatListis for long, dynamic lists, providing performance benefits through virtualization.
Code Example
How this code works
This code example demonstrates how to build a basic, scrollable user interface using several core React Native components. It combines different display elements into a single screen, allowing more content than initially visible to be accessed by swiping.
The main container is a ScrollView, which enables its children to be scrollable if their total height exceeds the screen's available space. It's styled with flex: 1 to ensure it expands and fills the entire screen, a crucial step beginners often miss for ScrollView to function correctly. Inside, View components act as flexible containers, grouping other elements like the header text and image, or the main body text. The Text component is used to display various pieces of static text, with different style props to change their appearance (like size, weight, or alignment). An Image component displays a picture, using the source={{ uri: '...' }} property to load an image from a web address, and style to set its dimensions.