Skip to main content

Lesson 10 of 10

Practice & Quiz - React Native Performance

Test your understanding of re-render profiling, image optimization, memory management, and the New Architecture's performance impact with a practical exercise and 5 quiz questions.

10 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Practical Exercise - Performance-Optimized Image Gallery

Practical Exercise - Performance-Optimized Image Gallery
import { memo, useCallback, useState } from 'react';
import {
  FlatList, Image, Pressable, StyleSheet,
  View, Text, useWindowDimensions,
} from 'react-native';
import { Image as ExpoImage } from 'expo-image';

interface GalleryItem { id: string; thumbUri: string; fullUri: string; title: string; }

// ─── ExpoImage vs built-in Image ──────────────────────────────
// expo-image: blurhash placeholders, progressive loading, disk+memory cache,
// better GIF/WebP support, and format auto-negotiation (AVIF on supported devices)

const GalleryItemView = memo(({
  item, size, onPress,
}: { item: GalleryItem; size: number; onPress: (id: string) => void }) => {
  const handlePress = useCallback(() => onPress(item.id), [item.id, onPress]);

  return (
    <Pressable onPress={handlePress} style={{ width: size, height: size }}>
      <ExpoImage
        source={{ uri: item.thumbUri }}
        style={styles.image}
        contentFit="cover"
        // blurhash shows a placeholder color while loading - no grey box flash
        placeholder={{ blurhash: 'L6PZfSi_.AyE_3t7t7R**0o#DgR4' }}
        // transition: fade in smoothly when loaded
        transition={200}
        // cachePolicy: disk+memory cache - avoids re-downloading on scroll
        cachePolicy="memory-disk"
        // recyclingKey: reuses the native image view on scroll (important for large lists)
        recyclingKey={item.id}
      />
      <Text style={styles.title} numberOfLines={1}>{item.title}</Text>
    </Pressable>
  );
});

export function PhotoGallery({ items }: { items: GalleryItem[] }) {
  const { width } = useWindowDimensions();
  const columns = 3;
  const itemSize = Math.floor(width / columns) - 4;
  const [selected, setSelected] = useState<string | null>(null);

  const renderItem = useCallback(
    ({ item }: { item: GalleryItem }) => (
      <GalleryItemView item={item} size={itemSize} onPress={setSelected} />
    ),
    [itemSize]
  );

  return (
    <>
      <FlatList
        data={items}
        renderItem={renderItem}
        keyExtractor={item => item.id}
        numColumns={columns}
        key={columns}
        initialNumToRender={12}
        maxToRenderPerBatch={12}
        windowSize={3}
        getItemLayout={(_, index) => ({
          length: itemSize,
          offset: itemSize * Math.floor(index / columns),
          index,
        })}
      />
      {selected && (
        <Pressable style={styles.modal} onPress={() => setSelected(null)}>
          <ExpoImage
            source={{ uri: items.find(i => i.id === selected)?.fullUri }}
            style={styles.fullImage}
            contentFit="contain"
            transition={300}
          />
        </Pressable>
      )}
    </>
  );
}

const styles = StyleSheet.create({
  image: { flex: 1 },
  title: { position: 'absolute', bottom: 0, left: 0, right: 0, backgroundColor: 'rgba(0,0,0,0.5)', color: '#fff', fontSize: 11, padding: 4 },
  modal: { ...StyleSheet.absoluteFillObject, backgroundColor: 'rgba(0,0,0,0.9)', justifyContent: 'center' },
  fullImage: { width: '100%', height: '80%' },
});

Module Quiz

Module Quiz
Question 1 of 5

Why use `expo-image` instead of React Native's built-in `Image` for production apps?

Tip

Tip

Practice Practice Quiz React Native Performance 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 9 Knowledge Check: Fabric, JSI, FlatList tuning, memory leak prevention, and Clean Architecture

Practice Task

Note

Practice Task - (1) Write a working example of Practice Quiz React Native Performance 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 Performance 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