← Back to Chapters

View Component in React Native

? View Component in React Native

? Quick Overview

The View component in React Native is the fundamental building block for creating layouts. It works like a container that supports layout using Flexbox, styling, touch handling, and accessibility controls.

? Key Concepts

  • Acts as a container for other components
  • Uses Flexbox for layout by default
  • Supports styling via the StyleSheet API
  • Platform-independent UI building block

? Syntax / Theory

The View component is imported from react-native and is used to group and style child components such as Text, Image, or other Views.

? Code Example(s)

? View Code Example
// Basic View container example
import React from "react";
import { View, Text } from "react-native";

export default function App() {
  return (
    
      Hello React Native
    
  );
}

? Live Output / Explanation

This code renders a simple container with text inside it. The View acts as a wrapper that controls layout and positioning.

? Interactive Example

Adjust the properties below to see how a <View> uses Flexbox to arrange its children. In React Native, flexDirection defaults to column.

1
2
3
// Generated React Native Code
<View style={{
  flex: 1,
  flexDirection: 'column',
  justifyContent: 'flex-start',
  alignItems: 'stretch',
}}>
  <Text>1</Text>
  <Text>2</Text>
  <Text>3</Text>
</View>

? Use Cases

  • Creating screen layouts
  • Grouping UI elements
  • Applying Flexbox-based design
  • Building reusable UI sections

? Tips & Best Practices

  • Keep Views lightweight and well-structured
  • Prefer StyleSheet over inline styles
  • Use nested Views for complex layouts

? Try It Yourself

  • Create a card layout using multiple Views
  • Apply Flexbox properties to a View
  • Nest Text and Image inside a View