Skip to content
Tutorials Center
My learning

Topics

Lesson 10 of 71 ยท Styling and Layout

Responsive Layouts and Screen Sizes

Build React Native layouts that adapt to phones, tablets and foldables with useWindowDimensions, breakpoints, orientation, safe-area insets and PixelRatio.

Phones come in dozens of sizes, tablets are twice as wide, foldables change size while your app is running, and users rotate devices and split the screen. A layout that only looks right on the phone you test with will break for a large share of your audience. Fortunately, React Native gives you a small, solid toolkit: a hook that reports the window size, a library that reports the unsafe edges of the screen, and a few helpers for pixel density.

This lesson shows how to read the window size correctly, how to define breakpoints, how to respect safe areas such as the notch and the home indicator, and how to think about tablets and foldables. As always, native APIs appear as read-only code, and the demos are browser stand-ins for the calculations.

Reading the window size

The tool you will use most is the useWindowDimensions hook:

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

export function SizeLabel() {
  const { width, height, scale, fontScale } = useWindowDimensions();
  return <Text>{width} x {height} dp, scale {scale}, font scale {fontScale}</Text>;
}

It returns four numbers:

  • width and height of the app window, in density-independent pixels.
  • scale: physical pixels per dp (for example 2 or 3).
  • fontScale: the user's text-size preference multiplier (1 is the default).

The hook re-renders your component whenever any of them changes: rotation, entering split-screen, unfolding a device, changing the font size in system settings. That is the main reason to prefer it over the older imperative API, Dimensions.get('window'), which returns a snapshot taken at the moment of the call. A StyleSheet.create block that reads Dimensions.get('window').width at the top of a file is computed once and never updates, which is a classic source of "it broke after rotating" bugs.

Dimensions.get('screen') reports the whole display, including areas the system bars cover, and is rarely what you want for layout. Use 'window' or, better, the hook.

Do not branch on device type

It is tempting to ask "is this a tablet?" and then switch layouts. That question has no stable answer: an Android tablet can run your app in a narrow split-screen window, a foldable is a phone one minute and a tablet the next, and a phone in landscape is wider than an old tablet. Base decisions on the window width you actually have.

The usual approach is a small set of breakpoints, in the style of Material Design's window size classes:

Class Width in dp Typical layout
compact below 600 one column, bottom tabs
medium 600 to 839 two columns, navigation rail
expanded 840 and above list and detail side by side

Put the thresholds in one helper so the whole app agrees:

JS A breakpoint helper (plain JavaScript)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice 599 and 600: the boundary is a single dp, so a layout never sits in an undefined zone. Keep the helper free of React, and your tests (see Testing React Native Apps) can call it directly.

Measure, do not guess

Window, not device Split-screen and foldables change the width.
useWindowDimensions Re-renders on every change.
Dimensions.get A snapshot that goes stale.
1 / 2

Orientation

There is no orientation property to read: you compare width and height.

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

Whether your app may rotate at all is a build setting. In an Expo project the orientation field of the app config accepts portrait, landscape or default (both). New projects commonly start locked to portrait, so rotation only happens after you change that setting. Locking a phone app to portrait is fine, but tablets and foldables are used in every orientation, so test them unlocked. To lock or unlock at runtime for one screen (a video player, say), use the expo-screen-orientation library.

Safe areas

Modern phones have notches, camera cut-outs, rounded corners and gesture bars that the app window extends underneath. The safe area is the rectangle that none of those cover. The library react-native-safe-area-context (included in Expo templates and a dependency of React Navigation) gives you the insets:

TSX
import { SafeAreaProvider, useSafeAreaInsets } from 'react-native-safe-area-context';

function Footer() {
  const insets = useSafeAreaInsets(); // { top, right, bottom, left } in dp
  return (
    <View style={{ paddingBottom: Math.max(insets.bottom, 12), paddingHorizontal: 16 }}>
      <Text>Always above the home indicator</Text>
    </View>
  );
}

export default function App() {
  return (
    <SafeAreaProvider>
      <Footer />
    </SafeAreaProvider>
  );
}

Wrap the app in SafeAreaProvider once, near the root. Then either use the library's SafeAreaView component for simple screens, or useSafeAreaInsets when you need fine control, as above (a full-bleed image that goes under the status bar but a title that does not). Math.max(insets.bottom, 12) is a common idiom: use the inset when there is one, and a minimum padding on devices without it.

The SafeAreaView that comes with React Native itself only handles iOS and is deprecated in recent versions, so import it from the library instead. Navigators from React Navigation apply insets to their headers and tab bars automatically; you only handle the insets of your own content.

PixelRatio

PixelRatio answers questions about physical pixels:

  • PixelRatio.get() returns the same density as scale above.
  • PixelRatio.getFontScale() returns the user's font scale.
  • PixelRatio.roundToNearestPixel(8.4) rounds a dp value to a whole number of physical pixels, which keeps thin lines crisp.
  • PixelRatio.getPixelSizeForLayoutSize(dp) converts dp to physical pixels, handy when you request an image from a server that should match the screen.

You rarely need them. A common use is choosing an image size: a 200 dp wide avatar on a 3x screen wants a 600-pixel-wide file.

Putting it together

The stand-in below mimics a screen. Drag the sliders to change the "window" size and see the breakpoint, orientation and grid change. It does what the real hook would do, except that here the numbers come from sliders instead of the device.

Stand-in: a breakpoint calculator

The sliders stop at 700 so that the grid fits the preview, which means you will see compact and medium; set the width at 600 and the grid changes from two to three columns at exactly that value. The "physical px" line is width * scale, the same conversion PixelRatio.getPixelSizeForLayoutSize performs.

Tips for tablets and foldables

  • Test with a resizable emulator (Android Studio offers resizable and foldable profiles; the iPad simulator supports multitasking sizes).
  • Make content readable, not just stretched. On wide windows, cap line length with maxWidth and center the content, or move to a list and detail layout.
  • Respond to size changes without losing state. Because the hook triggers a re-render and not a remount, a form in progress survives a fold or a rotation.
  • Scale text and touch targets with care. When the user raises the font scale, rows with a fixed height clip their text. Prefer minHeight and let rows grow. More in Accessibility in React Native.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is useWindowDimensions preferred over reading Dimensions.get('window') at the top of a file?

    The hook subscribes to changes (rotation, split-screen, fold). A value read once at module load stays stale.
  2. 2. Which approach is the most reliable way to choose a tablet layout?

    Windows change size on foldables and in split-screen, so the available width is the real signal, not the hardware.
  3. 3. What does useSafeAreaInsets return?

    The insets tell you how much space on each edge is covered by cut-outs, system bars or the home indicator, so you can pad your content.
  4. 4. A phone reports scale 3 and a window width of 400 dp. How many physical pixels wide is the window?

    Physical pixels equal dp times the pixel ratio: 400 x 3 = 1200.

Summary

  • Read the window with useWindowDimensions, which updates on rotation, split-screen and folding; a one-time Dimensions.get goes stale.
  • Decide layouts from window width with a shared breakpoint helper, not from guesses about the device type.
  • Orientation is width > height; whether the app can rotate is set in the app config.
  • Use react-native-safe-area-context (SafeAreaProvider, useSafeAreaInsets) to stay clear of notches and gesture bars.
  • PixelRatio converts between dp and physical pixels; it matters mainly for image sizes and crisp lines.
  • The calculators here are stand-ins that take sizes from sliders instead of a device.

Next: Platform-Specific Code