Course contentsShow
← React Native
Lesson 156 of 1,5553. Core Primitive ComponentsFree lesson

Handling Orientation Changes

Learners will adjust layout flow to accommodate dynamic shifts between portrait and landscape modes.

In this lesson, you will learn how to make your app's user interface fluid and functional as users rotate their devices between portrait and landscape modes.

The Challenge of Rotation

When a user tilts their phone, the available screen space changes significantly. A layout that looks perfect in a tall, narrow portrait mode often feels squashed or awkward when stretched across a wide, short landscape mode. Because React Native components are built using the Box Model and Hierarchical Interfaces, we need a strategy to adjust our layout flow dynamically when these dimensions shift.

Detecting the Change

To handle orientation changes, we rely on the useWindowDimensions hook. This tool constantly monitors the screen's current width and height. When the device rotates, these values update automatically, triggering a re-render of your component.

Think of it like a smart container: instead of hard-coding pixel values, you check the dimensions and update your styles accordingly.

Code Example

Here is how you can use a simple conditional check to change your layout flow from a column to a row based on orientation:

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

const ResponsiveLayout = () => {
  const { width, height } = useWindowDimensions();
  const isLandscape = width > height;

  return (
    <View style={{ 
      flexDirection: isLandscape ? 'row' : 'column',
      padding: 20 
    }}>
      <Text>Sidebar or Header</Text>
      <Text>Main Content Area</Text>
    </View>
  );
};

Why This Matters

If you don't adjust your layout, elements might bleed off the screen or overlap in ways that break your Surface-Level Component Rendering. By checking isLandscape (where width is greater than height), you can tell your components to rearrange themselves—for example, by moving a navigation menu from the top of the screen to the side, or by switching a single-column list into a grid.

Remember, adaptive layouts are about creating a consistent experience regardless of how the user chooses to hold their device. By combining useWindowDimensions with standard Flexbox properties, you ensure your app remains usable and professional in every orientation.

Key Takeaway: Use useWindowDimensions to dynamically recalculate your component's structure, allowing your layout flow to adapt instantly when the user rotates their device.