Welcome to Ray's Blog

Stay Hungry Stay Foolish - Steve Jobs

0%

Handling Safe Areas in React Native Expo: Avoiding Notches and Home Indicators

Have you ever launched your React Native app on a fancy new phone only to find your content hiding behind a notch or getting cut off by the home indicator? 😱

With today’s smartphones featuring notches, camera cutouts, gesture bars, and curved corners, it’s essential to design your app to respect these “danger zones.” In this tutorial, you’ll learn how to handle safe areas in React Native apps built with Expo using react-native-safe-area-context.

We’ll cover:

  • What safe areas are and why they matter
  • How to use SafeAreaProvider, SafeAreaView, and useSafeAreaInsets
  • Applying safe areas to scroll views, modals, custom headers/footers
  • Best practices and common pitfalls

Let’s dive in! 🚀


🛡️ What Are Safe Areas and Why Should You Care?

Safe areas are parts of the screen not obstructed by device hardware or OS UI elements — like notches, status bars, home indicators, or navigation bars. If your content lands in those zones, it could become hard to read or interact with.

Safe areas dynamically adjust margins/padding so that your app content stays visible, readable, and tap-friendly across all devices.


🧰 Setting Up SafeAreaProvider in Expo

Expo apps include the react-native-safe-area-context library by default. If not, install it with:

1
expo install react-native-safe-area-context

Wrap your root component in SafeAreaProvider:

1
2
3
4
5
6
7
8
9
10
11
import React from 'react';
import { SafeAreaProvider } from 'react-native-safe-area-context';
import HomeScreen from './HomeScreen';

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

This ensures that all child components can access safe area data.

✅ Using SafeAreaView to Respect Safe Zones

Replace your components with SafeAreaView to automatically apply appropriate padding:

1
2
3
4
5
6
7
8
9
import { SafeAreaView, StyleSheet, Text } from 'react-native-safe-area-context';

export default function HomeScreen() {
return (
<SafeAreaView style={styles.container}>
<Text style={styles.title}>Hello, safe world!</Text>
</SafeAreaView>
);
}

🧠 Custom Layouts with useSafeAreaInsets

Need fine-tuned control? Use the useSafeAreaInsets hook:

1
2
3
import { useSafeAreaInsets } from 'react-native-safe-area-context';

const insets = useSafeAreaInsets();

Then apply insets directly to your component styles. This is great for headers, footers, or any layout requiring edge-specific adjustments.

Example:

1
2
3
<View style={[styles.header, { paddingTop: insets.top }]}>
<Text>My Custom Header</Text>
</View>

📜 Scrollable Screens and Safe Areas

For ScrollView and FlatList content, you can:
Wrap the scroll view in SafeAreaView.
Or apply insets as padding using the hook.

Example:

1
2
3
4
5
6
7
8
<ScrollView
contentContainerStyle={{
paddingTop: insets.top,
paddingBottom: insets.bottom,
}}
>
{/* scroll content */}
</ScrollView>

🧩 Safe Areas in Modals

Modals are tricky because they might not inherit the safe area context. To fix this:

1
2
3
4
5
<Modal visible={visible}>
<SafeAreaProvider>
<SafeAreaView style={{ flex: 1 }}>{/* modal content */}</SafeAreaView>
</SafeAreaProvider>
</Modal>

This ensures the modal also respects the device’s safe zones.

🧱 Custom Headers and Footers with Safe Area Awareness

Use the edges prop to apply padding only where needed:

1
2
3
4
5
6
7
8
<SafeAreaView edges={['top', 'left', 'right']} style={styles.header}>
<Text>Header</Text>
</SafeAreaView>

<SafeAreaView edges={['bottom', 'left', 'right']} style={styles.footer}>
<Text>Footer</Text>
</SafeAreaView>

Best Practices and Common Pitfalls

✅ Always use at the root of your app.
✅ Import SafeAreaView from react-native-safe-area-context.
🚫 Avoid nesting SafeAreaViews unnecessarily.
🧪 Test on devices with and without notches (and in landscape mode).
📐 Use numeric padding values — avoid percentages.
🔁 If content disappears in a modal, add a SafeAreaProvider inside.

So next time you build that killer UI — make sure it lives in the safe zone. Your users (and their thumbs) will thank you! 🙌

Happy coding!

download