Lesson 62 of 60 – TypeScript with View
100%

TypeScript with View

In React Native, the View component is one of the most commonly used components. It works like a container that allows us to group and organize other components.

Note: TypeScript helps us define the types of props, state, styles, and functions used with React Native's View component.

1. What is View?

View is a basic React Native component used as a container for other components.

import { View } from "react-native";


    ...

It is commonly used to create layouts and group related UI elements.

2. Importing View

Before using View, import it from React Native.

import { View } from "react-native";

TypeScript understands the built-in React Native component and its properties.

3. Basic View Example

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

export default function App(): React.JSX.Element {
    return (
        <View>
            <Text>Hello React Native</Text>
        </View>
    );
}

Here, View acts as a container for the Text component.

4. View as a Container

A View can contain multiple React Native components.

<View>
    <Text>Name</Text>
    <Text>Age</Text>
    <Text>City</Text>
</View>

This makes View useful for organizing UI elements.

5. Styling a View

We can apply styles to a View using the style prop.

<View style={{ padding: 20 }}>
    <Text>Welcome</Text>
</View>

The style object controls the appearance and layout of the View.

6. Using StyleSheet with View

For larger applications, it is better to keep styles inside StyleSheet.create().

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

const styles = StyleSheet.create({
    container: {
        padding: 20
    }
});

The style can then be assigned to the View.

7. Applying a Style

<View style={styles.container}>
    ...
</View>

TypeScript can understand the value passed to the style prop.

8. View with Multiple Children

<View style={styles.container}>
    <Text>Student Name</Text>
    <Text>Rahul</Text>
    <Text>Course</Text>
    <Text>React Native</Text>
</View>

A View can contain several child components.

9. Creating a Typed View Component

We can create our own reusable component that returns a View.

function Box(): React.JSX.Element {
    return (
        <View>
            <Text>My Box</Text>
        </View>
    );
}

The return type describes that the component returns a JSX element.

10. Creating Props for a View Component

Custom View components can receive typed props.

interface BoxProps {
    title: string;
}

function Box({ title }: BoxProps): React.JSX.Element {
    return (
        <View>
            <Text>{title}</Text>
        </View>
    );
}

11. Using a Typed View Component

<Box title="Student Details" />

Because title is defined as a string, TypeScript can check whether the correct type is provided.

12. Optional Props with View

A custom View component can have optional properties.

interface BoxProps {
    title?: string;
}

The ? means the property does not have to be provided.

13. Boolean Props

Boolean props can control how a View behaves.

interface BoxProps {
    bordered: boolean;
}

The value must be true or false.

14. Conditional View Styling

A boolean prop can be used to apply different styles.

interface BoxProps {
    bordered: boolean;
}

function Box({ bordered }: BoxProps): React.JSX.Element {
    return (
        <View
            style={bordered
                ? styles.boxBorder
                : styles.box}
        >
            <Text>Content</Text>
        </View>
    );
}

15. View with Flexbox

React Native uses Flexbox for arranging components inside a View.

const styles = StyleSheet.create({
    container: {
        flexDirection: "row",
        justifyContent: "center",
        alignItems: "center"
    }
});

TypeScript checks the values used in the style definition.

16. View with Row Layout

<View style={styles.row}>
    <Text>One</Text>
    <Text>Two</Text>
    <Text>Three</Text>
</View>

A row layout places child components horizontally.

17. View with Column Layout

The default Flexbox direction in React Native is column.

<View style={styles.column}>
    <Text>Name</Text>
    <Text>Email</Text>
    <Text>Phone</Text>
</View>

The components are displayed vertically.

18. Nested Views

A View can contain another View.

<View style={styles.container}>

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

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

</View>

Nested Views are commonly used to create complex layouts.

19. View with State

A View can display content based on component state.

const [visible, setVisible] = useState<boolean>(true);

{visible && (
    <View>
        <Text>This View is visible</Text>
    </View>
)}

TypeScript ensures that visible is a boolean.

20. View with Conditional Rendering

{isLoggedIn ? (
    <View>
        <Text>Welcome User</Text>
    </View>
) : (
    <View>
        <Text>Please Login</Text>
    </View>
)}

Conditional rendering allows different Views to be displayed according to application state.

21. View with onPress Components

View itself is mainly a layout component. For press interactions, components such as Pressable are normally used.

<Pressable onPress={() => console.log("Pressed")}>
    <View>
        <Text>Press Me</Text>
    </View>
</Pressable>

22. Reusable Container Component

A typed View component can be created as a reusable container.

interface ContainerProps {
    children: React.ReactNode;
}

function Container({
    children
}: ContainerProps): React.JSX.Element {

    return (
        <View style={styles.container}>
            {children}
        </View>
    );
}

React.ReactNode allows different types of React content to be passed as children.

23. Using a Reusable Container

<Container>
    <Text>Student Profile</Text>
</Container>

This approach helps create reusable and consistent layouts.

24. View with Array Data

Data can be mapped into Views when creating repeated UI.

const students: string[] = [
    "Rahul",
    "Amit",
    "Priya"
];

{students.map((student: string) => (
    <View key={student}>
        <Text>{student}</Text>
    </View>
))}

The array is explicitly typed as an array of strings.

25. View with Object Data

interface Student {
    id: number;
    name: string;
}

const student: Student = {
    id: 101,
    name: "Rahul"
};

<View>
    <Text>{student.name}</Text>
</View>

Interfaces help describe the structure of data displayed inside a View.

26. Common TypeScript Mistakes with View

  • Using incorrect prop types in custom View components.
  • Forgetting to type custom component props.
  • Using incorrect style property values.
  • Forgetting required props.
  • Using undefined data without checking it.

Proper TypeScript types help identify these problems during development.

27. Best Practices for Typed Views

  • Use interfaces for complex component props.
  • Use reusable container components.
  • Keep styles organized with StyleSheet.
  • Use meaningful prop names.
  • Type arrays and objects used by the View.
  • Avoid unnecessary use of any.

28. Complete Typed View Example

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

interface ProfileProps {
    name: string;
    course: string;
}

function Profile({
    name,
    course
}: ProfileProps): React.JSX.Element {

    return (
        <View style={styles.card}>
            <Text>Name: {name}</Text>
            <Text>Course: {course}</Text>
        </View>
    );
}

export default function App(): React.JSX.Element {
    return (
        <View style={styles.container}>
            <Profile
                name="Rahul"
                course="React Native"
            />
        </View>
    );
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        padding: 20
    },
    card: {
        padding: 20,
        borderWidth: 1,
        borderRadius: 10
    }
});

29. Why TypeScript is Useful with View

TypeScript makes custom View components easier to maintain by checking props, data structures, and function types.

  • Better code checking
  • Safer component props
  • Better editor suggestions
  • Easier refactoring
  • Fewer type-related mistakes

30. Summary

The View component is the basic layout container in React Native. When working with TypeScript, we can strongly type custom View component props, children, state, arrays, objects, and functions.

Understanding typed Views is important before creating larger React Native interfaces.

📌 Key Points

  • View is a basic container component in React Native.
  • View can contain multiple child components.
  • TypeScript can type custom View component props.
  • Interfaces are useful for complex View props.
  • React.ReactNode can be used for children.
  • StyleSheet can be used to organize View styles.
  • View supports Flexbox-based layouts.
  • Typed data can safely be displayed inside Views.

🧠 Quick Quiz

Question: Which React Native component is commonly used as a container for other components?