Labs ICT
โญ Pro Login

Core Components

Understanding View, Text, Image, ScrollView and more

Core Components in React Native

React Native provides a set of built-in components that map to native platform views. These are the building blocks you'll use to create your app's interface.

The Basics


  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
  โ”‚           Core Components                       โ”‚
  โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
  โ”‚                                                  โ”‚
  โ”‚          โ†’ Like a 
(container) โ”‚ โ”‚ โ†’ Like a

(text display) โ”‚ โ”‚ โ†’ Like (image display) โ”‚ โ”‚ โ†’ Like (text input) โ”‚ โ”‚ โ†’ Scrollable container โ”‚ โ”‚ โ†’ Pressable element โ”‚ โ”‚ โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Example: A Simple Card Component


import React from 'react';
import { View, Text, Image, TouchableOpacity, StyleSheet } from 'react-native';

const UserCard = ({ name, avatar, bio }) => {
  return (
    <View style={styles.card}>
      <Image source={{ uri: avatar }} style={styles.avatar} />
      <Text style={styles.name}>{name}</Text>
      <Text style={styles.bio}>{bio}</Text>
      <TouchableOpacity style={styles.button}>
        <Text style={styles.buttonText}>Follow</Text>
      </TouchableOpacity>
    </View>
  );
};

const styles = StyleSheet.create({
  card: { padding: 16, backgroundColor: '#fff', borderRadius: 8 },
  avatar: { width: 80, height: 80, borderRadius: 40 },
  name: { fontSize: 18, fontWeight: 'bold', marginTop: 8 },
  bio: { fontSize: 14, color: '#666', marginTop: 4 },
  button: { marginTop: 12, padding: 8, backgroundColor: '#007AFF' },
  buttonText: { color: '#fff', textAlign: 'center' },
});

Lists and FlatList

For displaying lists of data, use FlatList instead of mapping over arrays. FlatList only renders items visible on screen, making it efficient for long lists.


import { FlatList, Text, View } from 'react-native';

const TodoList = ({ items }) => {
  return (
    <FlatList
      data={items}
      keyExtractor={(item) => item.id.toString()}
      renderItem={({ item }) => (
        <View style={styles.todoItem}>
          <Text>{item.text}</Text>
        </View>
      )}
    />
  );
};

๐Ÿงช Quick Quiz

What is a key in React Native lists used for?