Lesson 12 of 60 – View Component in React Native
20%

View Component in React Native

The View component is one of the most important and commonly used components in React Native. It is primarily used as a container for other components and can be used to create layouts, groups of content and sections of a mobile screen.

Note: If you have worked with HTML, you can think of View as a general-purpose container. However, React Native View is not an HTML div. It is a React Native component designed for mobile interfaces.

1. What is View?

View is a fundamental React Native component used to contain and organize other components.

import { View } from "react-native";

It is commonly used to create the structure of a mobile screen.

2. Importing View

Before using View, import it from the react-native package.

import { View } from "react-native";

You can import multiple React Native components in the same statement.

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

3. Basic View Syntax

<View>

</View>

The opening and closing tags define the boundaries of the View component.

4. View with Text

A View can contain a Text component.

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

export default function App() {

    return (
        <View>
            <Text>
                Hello React Native
            </Text>
        </View>
    );
}

5. View as a Container

The most common purpose of View is to act as a container for other components.

<View>

    <Text>Student Name</Text>

    <Text>React Native</Text>

</View>

Both Text components are children of the View.

6. Multiple Views

A View can contain other Views.

<View>

    <View>
        <Text>Header</Text>
    </View>

    <View>
        <Text>Content</Text>
    </View>

</View>

This allows developers to create nested layouts.

7. Nested Views

Nesting means placing one View inside another View.

<View>

    <View>

        <Text>
            Student Information
        </Text>

    </View>

</View>

Nested Views are commonly used for organizing complex interfaces.

8. Styling a View

A View can receive a style prop.

<View
    style={{
        padding: 20
    }}
>

    <Text>Hello</Text>

</View>

The style controls the appearance and layout of the View.

9. Using StyleSheet with View

For larger applications, StyleSheet can be used to organize styles.

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

const styles = StyleSheet.create({

    container: {
        padding: 20
    }

});

10. Applying a StyleSheet Style

<View style={styles.container}>

    <Text>
        Welcome
    </Text>

</View>

The container style is applied to the View.

11. Width and Height

You can specify the width and height of a View.

const styles = StyleSheet.create({

    box: {
        width: 200,
        height: 100
    }

});

This can be useful for creating cards, panels and other UI sections.

12. Background Color

The backgroundColor property changes the background of a View.

const styles = StyleSheet.create({

    box: {
        backgroundColor: "lightblue",
        padding: 20
    }

});

13. Border Radius

The borderRadius property can be used to create rounded corners.

const styles = StyleSheet.create({

    card: {
        backgroundColor: "white",
        borderRadius: 10,
        padding: 20
    }

});

Rounded Views are commonly used for cards and panels.

14. Padding

Padding creates space between the content and the inside edge of a View.

const styles = StyleSheet.create({

    box: {
        padding: 20
    }

});

Padding can also be specified separately for different sides.

15. Margin

Margin creates space outside a View.

const styles = StyleSheet.create({

    box: {
        margin: 20
    }

});

Margins are useful for creating space between UI elements.

16. Flexbox with View

React Native uses Flexbox for many layout requirements.

const styles = StyleSheet.create({

    container: {
        flex: 1,
        justifyContent: "center",
        alignItems: "center"
    }

});

This places the content in the center of the available space.

17. Row Layout

The flexDirection property can be used to arrange children horizontally.

const styles = StyleSheet.create({

    row: {
        flexDirection: "row"
    }

});
<View style={styles.row}>

    <View>
        <Text>One</Text>
    </View>

    <View>
        <Text>Two</Text>
    </View>

</View>

18. Column Layout

The default Flexbox direction in React Native is generally column.

const styles = StyleSheet.create({

    column: {
        flexDirection: "column"
    }

});

This places children vertically.

19. justifyContent

justifyContent controls the arrangement of children along the main axis.

container: {
    flex: 1,
    justifyContent: "center"
}

Common values include:

  • flex-start
  • center
  • flex-end
  • space-between
  • space-around
  • space-evenly

20. alignItems

alignItems controls the alignment of children along the cross axis.

container: {
    flex: 1,
    alignItems: "center"
}

This is commonly used together with justifyContent.

21. View with Image

A View can contain an Image component.

import {
    View,
    Image
} from "react-native";

<View>

    <Image
        source={require("./assets/logo.png")}
        style={{
            width: 100,
            height: 100
        }}
    />

</View>

22. View with TextInput

A View can contain input fields.

import {
    View,
    TextInput
} from "react-native";

<View>

    <TextInput
        placeholder="Enter your name"
    />

</View>

This structure is commonly used when creating forms.

23. View with Pressable

A View can also contain interactive components such as Pressable.

<View>

    <Pressable
        onPress={() => alert("Pressed")}
    >

        <Text>
            Press Me
        </Text>

    </Pressable>

</View>

24. View for a Card

View can be used to create reusable card-like interfaces.

const styles = StyleSheet.create({

    card: {
        padding: 20,
        margin: 10,
        borderRadius: 10,
        backgroundColor: "white"
    }

});
<View style={styles.card}>

    <Text>
        Student Details
    </Text>

    <Text>
        Rahul
    </Text>

</View>

25. View for Screen Sections

A mobile screen can be divided into multiple View sections.

<View style={styles.container}>

    <View style={styles.header}>
        <Text>Header</Text>
    </View>

    <View style={styles.content}>
        <Text>Content</Text>
    </View>

    <View style={styles.footer}>
        <Text>Footer</Text>
    </View>

</View>

26. View and Safe Area

Mobile screens can contain areas occupied by system interface elements such as status bars and device notches.

For appropriate screen layouts, React Native applications can use safe-area solutions such as the safe area APIs provided by the React Native ecosystem.

This helps important content avoid being placed underneath system interface areas.

27. Complete View Example

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

export default function App() {

    return (
        <View style={styles.container}>

            <View style={styles.header}>
                <Text style={styles.title}>
                    Student Dashboard
                </Text>
            </View>

            <View style={styles.card}>

                <Text>
                    Student Name: Rahul
                </Text>

                <Text>
                    Course: React Native
                </Text>

            </View>

        </View>
    );
}

const styles = StyleSheet.create({

    container: {
        flex: 1,
        padding: 20
    },

    header: {
        padding: 20,
        backgroundColor: "lightblue",
        borderRadius: 10
    },

    title: {
        fontSize: 22,
        fontWeight: "bold"
    },

    card: {
        marginTop: 20,
        padding: 20,
        borderRadius: 10,
        backgroundColor: "white"
    }

});

28. View vs HTML div

Feature React Native View HTML div
Environment Mobile applications Web applications
Package react-native HTML
Typical Use Container / layout Container / layout
Styling React Native style system CSS
Layout Flexbox-based CSS layout

The concepts are similar, but the components and styling systems are different.

29. Best Practices for View

  • Use View to organize screen sections.
  • Create reusable components when layouts repeat.
  • Use StyleSheet for organized styles.
  • Keep deeply nested layouts understandable.
  • Use Flexbox for responsive layouts.
  • Separate complex screen sections into components.
  • Use meaningful component and style names.

30. Complete View Workflow

View
 ↓
Container
 ↓
 ┌───────────────┐
 ↓               ↓
Text          Image
 ↓
TextInput
 ↓
Pressable
 ↓
Nested View
 ↓
Complete Screen

The View component is a foundation for building React Native layouts. As you progress through the course, you will combine View with Text, Image, TextInput, Pressable, ScrollView, FlatList and other components to create complete mobile application screens.

📌 Key Points

  • View is a fundamental React Native container component.
  • It is used to organize and group other components.
  • Views can contain Text, Images, inputs, buttons and other Views.
  • Views can be nested to create complex layouts.
  • The style prop controls the appearance and layout of a View.
  • StyleSheet can be used to organize styles.
  • React Native commonly uses Flexbox for layout.
  • justifyContent controls alignment along the main axis.
  • alignItems controls alignment along the cross axis.
  • View is similar in purpose to a web container, but it is not an HTML div.

🧠 Quick Quiz

Question: What is the primary purpose of the View component in React Native?