How to avoid notches with Safe Area Context in React Native apps

Published on Oct 14, 2021

5 min read


Most devices nowadays come with a notch or some kind of status bar. Therefore, when building a mobile application using React Native, it is vital to ensure that the content of an app screen is rendered correctly across different types of devices.

In this article, let's take a look at two different approaches to make app screens in React Native to avoid the content being positioned behind a notch or status bar.

The first approach will discuss SafeAreaView component from React Native components API. The second approach will discuss the advantage of using react-native-safe-area-context open source library and how it provides a cross-platform solution.

The Notch Problem


When you are starting to build a screen in React Native app, you might add use the following code snippet to display text:

1import React from 'react';
2import { StyleSheet, Text, View } from 'react-native';
4export const HomeScreen = () => {
5 return (
6 <View style={[styles.container]}>
7 <View style={{ backgroundColor: 'blue' }}>
8 <Text style={{ fontSize: 28, color: 'white' }}>Hello World</Text>
9 </View>
10 </View>
11 );
14const styles = StyleSheet.create({
15 container: {
16 flex: 1,
17 backgroundColor: 'red'
18 }

The above code snippet has a parent View component with a background color of red. It wraps another View component with a background color of blue that contains a Text component to display some text on the screen.

This will display the content of the app screen on an iOS device as shown below:


The contents of the nested View component hides behind the status bar and the notch on the iOS device.

On an Android device, the behavior is exactly the same:


How to Use the SafeAreaView component from React Native


One approach is to use SafeAreaView component available in React Native.

1import { SafeAreaView } from 'react-native';

It can be used in the place of the top-level View component. It renders content within the safe area boundaries around the nested content and automatically applies padding.

Modify the previous code snippet:

1import React from 'react';
2import { StyleSheet, Text, View, SafeAreaView } from 'react-native';
4export const HomeScreen = () => {
5 return (
6 <SafeAreaView style={[styles.container]}>
7 <View style={{ backgroundColor: 'blue' }}>
8 <Text style={{ fontSize: 28, color: 'white' }}>Hello World</Text>
9 </View>
10 </SafeAreaView>
11 );

It works perfectly on iOS:


In React Native, this component is only applicable to iOS devices with iOS version 11 or later. Unfortunately, that means it doesn't work for Android devices as the screen's content is still behind the status bar.

How to Use React Native's Safe Area Context Library


To use a cross-platform solution to handle safe areas on notch devices, there is a library called react-native-safe-area-context. It provides a flexible API to handle safe area insets in JS and works on iOS, Android, and Web.

Start by installing it in your React Native app:

# for plain React Native apps
yarn add react-native-safe-area-context
# install pod dependency for iOS only
npx pod-install
# for Expo apps
expo install react-native-safe-area-context

This library provides a SafeAreaProvider that needs to wrap either your Root Navigator or the screen where you want to handle safe area insets.

For example, in the code snippet below, the SafeAreaProvider wraps the HomeScreen component since there is only one screen in the example app.

1import React from 'react';
2import { SafeAreaProvider } from 'react-native-safe-area-context';
4import { HomeScreen } from './src/screens';
6export default function App() {
7 return (
8 <SafeAreaProvider>
9 <HomeScreen />
10 </SafeAreaProvider>
11 );

Now, you can import the SafeAreaView component from the react-native-safe-area-context library and replace it with the one from React Native.

1import React from 'react';
2import { StyleSheet, Text, View } from 'react-native';
3import { SafeAreaView } from 'react-native-safe-area-context';
5export const HomeScreen = () => {
6 return (
7 <SafeAreaView style={[styles.container]}>
8 <View style={{ backgroundColor: 'blue' }}>
9 <Text style={{ fontSize: 28, color: 'white' }}>Hello World</Text>
10 </View>
11 </SafeAreaView>
12 );
15const styles = StyleSheet.create({
16 container: {
17 flex: 1,
18 backgroundColor: 'red'
19 }

It works both for iOS and Android:


If you give the nested View component, a property of flex: 1 as:

1<View style={{ backgroundColor: 'blue', flex: 1 }}>

You can observe the safe are edges for iOS:


The SafeAreaView acts like a regular View component from React Native and includes additional padding to position the content after the notch or the status bar of a device.

It also comes with an edges prop that customizes safe area insets around different edges such as top, bottom, left, and right.

How to Use the useSafeAreaInsets Hook


Another advantage of using this library is that it provides a hook called useSafeAreaInsets. It offers more flexibility. It also gives more control, and you can apply padding for each edge using a property from this hook. For example, a View component below uses only wants the padding to be applied at the top edge:

1import { useSafeAreaInsets } from 'react-native-safe-area-context';
3export const HomeScreen = () => {
4 const insets = useSafeAreaInsets();
6 return (
7 <View
8 style={{
9 paddingTop:
10 }}
11 >
12 {children}
13 </View>
14 );



Handling status bars and notches across different devices become seamless with the react-native-safe-area-context library. Try it out in your next React Native app.

Source code at this GitHub repository

This post was also published at freeCodeCamp.

More Posts

Browse all posts

Aman Mittal author

I'm a software developer and a technical writer. On this blog, I write about my learnings in software development and technical writing.

Currently, working maintaining docs at 𝝠 Expo. Read more about me on the About page.

Copyright ©  2019-2024 Aman Mittal · All Rights Reserved.