Skip to main content

Beginner-Friendly Topic

Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!

Lesson 10 of 10

Practice & Quiz - React Native UI

Test your knowledge of FlatList optimization, SafeAreaView, responsive scaling, and Pressable with a practical exercise and 5 quiz questions.

10 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Practical Exercise - Optimized Product List with FlatList

Practical Exercise - Optimized Product List with FlatList
import React, { useCallback, memo } from 'react';
import {
  FlatList, View, Text, Image, Pressable,
  StyleSheet, useWindowDimensions, ListRenderItemInfo,
} from 'react-native';
import { SafeAreaView } from 'react-native-safe-area-context';

interface Product {
  id: string;
  name: string;
  price: number;
  imageUri: string;
  inStock: boolean;
}

// memo - prevents re-render when parent FlatList re-renders but item data unchanged
const ProductItem = memo(({ item, onPress }: { item: Product; onPress: (id: string) => void }) => {
  const handlePress = useCallback(() => onPress(item.id), [item.id, onPress]);

  return (
    <Pressable
      style={({ pressed }) => [styles.item, pressed && styles.itemPressed, !item.inStock && styles.itemOOS]}
      onPress={handlePress}
      accessibilityRole="button"
      accessibilityState={{ disabled: !item.inStock }}
    >
      <Image source={{ uri: item.imageUri }} style={styles.thumb} />
      <View style={styles.itemInfo}>
        <Text style={styles.itemName} numberOfLines={2}>{item.name}</Text>
        <Text style={styles.itemPrice}>${item.price.toFixed(2)}</Text>
        {!item.inStock && <Text style={styles.oos}>Out of stock</Text>}
      </View>
    </Pressable>
  );
});

interface ProductListProps {
  products: Product[];
  onProductPress: (id: string) => void;
}

export function ProductList({ products, onProductPress }: ProductListProps) {
  const { width } = useWindowDimensions();
  const numColumns = width >= 768 ? 3 : 2;

  // keyExtractor - must return stable unique strings; avoid using index
  const keyExtractor = useCallback((item: Product) => item.id, []);

  // renderItem wrapped in useCallback - stable reference for FlatList optimisation
  const renderItem = useCallback(
    ({ item }: ListRenderItemInfo<Product>) => (
      <ProductItem item={item} onPress={onProductPress} />
    ),
    [onProductPress]
  );

  return (
    <SafeAreaView style={styles.safe} edges={['top', 'left', 'right']}>
      <FlatList
        data={products}
        renderItem={renderItem}
        keyExtractor={keyExtractor}
        numColumns={numColumns}
        key={numColumns}               // forces re-mount when columns change
        initialNumToRender={10}        // render only visible items first
        maxToRenderPerBatch={10}       // items rendered per JS frame
        windowSize={5}                 // render 5 screens worth of items
        removeClippedSubviews          // unmount off-screen items (Android)
        getItemLayout={(_, index) => ({ // skip measurement - faster scroll
          length: ITEM_HEIGHT,
          offset: ITEM_HEIGHT * index,
          index,
        })}
        ListEmptyComponent={<Text style={styles.empty}>No products found</Text>}
        contentContainerStyle={styles.list}
      />
    </SafeAreaView>
  );
}

const ITEM_HEIGHT = 160;
const styles = StyleSheet.create({
  safe: { flex: 1, backgroundColor: '#f5f5f5' },
  list: { padding: 8 },
  item: { flex: 1, margin: 8, backgroundColor: '#fff', borderRadius: 12, overflow: 'hidden' },
  itemPressed: { opacity: 0.85 },
  itemOOS: { opacity: 0.5 },
  thumb: { width: '100%', height: 120, backgroundColor: '#e0e0e0' },
  itemInfo: { padding: 10 },
  itemName: { fontSize: 13, fontWeight: '500', color: '#1a1a1a' },
  itemPrice: { fontSize: 14, fontWeight: '700', color: '#229c6b', marginTop: 4 },
  oos: { fontSize: 11, color: '#ef4444', marginTop: 2 },
  empty: { textAlign: 'center', padding: 32, color: '#999' },
});

Module Quiz

Module Quiz
Question 1 of 5

Why is `keyExtractor` important in FlatList and why should you avoid using array index?

Tip

Tip

Practice Practice Quiz React Native UI in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Module 2 Knowledge Check: FlatList, safe areas, TextInput, and accessibility

Practice Task

Note

Practice Task - (1) Write a working example of Practice Quiz React Native UI from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Common Mistake

Warning

A common mistake with Practice Quiz React Native UI is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react native code.

Topics in This Module

0/10 done