React Native SafeAreaView for Safe Area Boundaries

React Native Safe Area

In this post, you will see the React Native SafeAreaView for Safe Area Boundaries. According to the definition, a SafeAreaView renders nested content and automatically applies paddings to reflect the portion of the view that is not covered by navigation bars, tab bars, toolbars, and other ancestor views.

For those who have no idea about the safe area, In iOS 7 Apple introduced the top layout guide and bottom layout guide properties which describes a screen area that isn’t covered by any content (status bar, navigation bar, toolbar, tab bar, etc.) but In iOS 11 Apple has deprecated these properties and introduced the safe area.

In simple words, it is the area where you don’t have to draw your imagination and have to keep it safe for native use.

By default when you make a React Native App you have seen that it renders the content on the status bar too

I can not be sure about the Android Emulator or iOS simulator as they can show it differently but yeah when you test the app on the real devices then you will see most of the devices will have no problem other than the notch devices like iOS 11. So for that either we can give the top padding of can use SafeAreaView which will do all these things for us. SafeAreaView will create a proper space and save the content overriding from the safe area.

To Import SafeAreaView in the Code

import { SafeAreaView} from 'react-native'

Use of SafeAreaView

Simply wrap your top-level view with a SafeAreaView with flex: 1 style applied to it. Do remember to provide top-level layout style to the view under SafeAreaView, not the SafeAreaView.

<SafeAreaView style={{ flex: 1 }}>
  <View style={{ flex: 1, paddingHorizontal: 20 }}>
    Other component here

To Make a React Native App

Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react-native init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli command line utility. Open the terminal and go to the workspace and run

npm install -g react-native-cli

Run the following commands to create a new React Native project

react-native init ProjectName

If you want to start a new project with a specific React Native version, you can use the --version argument:

react-native init ProjectName --version X.XX.X
react-native init ProjectName --version react-native@next

This will make a project structure with an index file named App.js in your project directory.

Code for React Native SafeAreaView

Open App.js in any code editor and replace the code with the following code.


//Example of SafeAreaView in React Native
import React from 'react';
//Import React
import { Text, View, SafeAreaView } from 'react-native';
//Import basic react native components

export default function App() {
  return (
    <SafeAreaView style={{ flex: 1 }}>
      <View style={{ flex: 1, paddingHorizontal: 20 }}>
        <Text style={{ fontSize: 20 }}>
          Example of SafeArea in Recat Native
        <Text style={{ fontSize: 20 }}>
          In publishing and graphic design, lorem ipsum is a placeholder text
          commonly used to demonstrate the visual... please add more content..

To Run the React Native App

Open the terminal again and jump into your project using.
cd ProjectName
To run the project on an Android Virtual Device or on real debugging device
react-native run-android
or on the iOS Simulator by running
react-native run-ios (macOS only).

Output Screenshots

Image   Image

Output in Online Emulator

That was the React Native SafeAreaView for safe area boundaries. If you have any doubts or you want to share something about the topic you can comment below or contact us here. The remaining components will be covered in the next article. Stay tuned!

Hope you liked it. 🙂

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.