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 3 of 10

Dimensions & useWindowDimensions

Mobile screens vary enormously - from small Android phones (360dp) to large iPads (1366dp). Dimensions API gives you screen metrics; useWindowDimensions is the hook version that automatically re-renders on orientation change.

20 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

useWindowDimensions - The Modern Approach

useWindowDimensions - The Modern Approach
import { useWindowDimensions, View, Text, StyleSheet } from 'react-native';

// ❌ OLD - Dimensions.get() is a snapshot, doesn't update on rotation
const { width } = Dimensions.get('window');

// ✅ MODERN - hook-based, auto-updates on orientation change
function ResponsiveGrid() {
  const { width, height } = useWindowDimensions();

  const isTablet = width >= 768;
  const isLandscape = width > height;
  const numColumns = isTablet ? (isLandscape ? 4 : 3) : 2;

  const itemWidth = (width - 16 * (numColumns + 1)) / numColumns;

  return (
    <FlatList
      data={items}
      numColumns={numColumns}
      key={numColumns}  // Force re-render when columns change (required by FlatList)
      renderItem={({ item }) => (
        <View style={[styles.item, { width: itemWidth }]}>
          <Text>{item.title}</Text>
        </View>
      )}
      keyExtractor={item => item.id}
      contentContainerStyle={{ padding: 16, gap: 16 }}
    />
  );
}

// ---- Responsive scale utility ----
const BASE_WIDTH = 375; // iPhone 14 Pro base design width

export function scale(size: number): number {
  const { width } = Dimensions.get('window');
  return (width / BASE_WIDTH) * size;
}

export function moderateScale(size: number, factor = 0.5): number {
  return size + (scale(size) - size) * factor;
}

// Usage: moderateScale(14) → 14dp on 375px phones, ~16dp on 430px phones
<Text style={{ fontSize: moderateScale(14) }}>Responsive text</Text>

Tip

Tip

Practice Dimensions useWindowDimensions in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Dynamic responsive dimensions that adapt to device rotation and split-screen

Practice Task

Note

Practice Task - (1) Write a working example of Dimensions useWindowDimensions 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.

Quick Quiz

Common Mistake

Warning

A common mistake with Dimensions useWindowDimensions 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