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.
View component.
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.
Before using View, import it from React Native.
import { View } from "react-native";
TypeScript understands the built-in React Native component and its properties.
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.
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.
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.
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.
<View style={styles.container}>
...
</View>
TypeScript can understand the value passed to the style prop.
<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.
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.
Custom View components can receive typed props.
interface BoxProps {
title: string;
}
function Box({ title }: BoxProps): React.JSX.Element {
return (
<View>
<Text>{title}</Text>
</View>
);
}
<Box title="Student Details" />
Because title is defined as a string, TypeScript can check
whether the correct type is provided.
A custom View component can have optional properties.
interface BoxProps {
title?: string;
}
The ? means the property does not have to be provided.
Boolean props can control how a View behaves.
interface BoxProps {
bordered: boolean;
}
The value must be true or false.
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>
);
}
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.
<View style={styles.row}>
<Text>One</Text>
<Text>Two</Text>
<Text>Three</Text>
</View>
A row layout places child components horizontally.
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.
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.
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.
{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.
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>
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.
<Container>
<Text>Student Profile</Text>
</Container>
This approach helps create reusable and consistent layouts.
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.
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.
Proper TypeScript types help identify these problems during development.
any.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
}
});
TypeScript makes custom View components easier to maintain by checking props, data structures, and function types.
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.
View is a basic container component in React Native.React.ReactNode can be used for children.Question: Which React Native component is commonly used as a container for other components?