Custom Components
In React Native, custom components are reusable UI building blocks that follow React's component-based architecture. They enable you to create modular, maintainable code by encapsulating specific functionality and styling into self-contained units that can be used throughout your application.
Creating Custom Components
- Create a
componentsfolder in your project to organize custom components - Create new files with
.tsxextension - Export components using standard React patterns
Example
import { Image, Text, View } from "react-native";
interface CustomCompProps {
name: string;
description?: string;
showImage?: boolean;
}
export default function CustomComponent({
name,
description,
showImage = false,
}: CustomCompProps) {
return (
<View>
{showImage ? (
<Image
source={{
uri: "https://avatars.githubusercontent.com/u/43630417?v=4",
}}
width={100}
height={100}
/>
) : null}
<Text style={{ fontSize: 30 }}>{name}</Text>
{description ? <Text>{description}</Text> : null}
</View>
);
}