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
  </View>
</SafeAreaView>

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 command line interface to make our React Native App.

If you have previously installed a global react-native-cli package, please remove it as it may cause unexpected issues:

npm uninstall -g react-native-cli @react-native-community/cli

Run the following commands to create a new React Native project

npx react-native init ProjectName

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

npx react-native init ProjectName --version X.XX.X

Note If the above command is failing, you may have old version of react-native or react-native-cli installed globally on your pc. Try uninstalling the cli and run the cli using npx.

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.

App.js

//React Native SafeAreaView for Safe Area Boundaries
//https://aboutreact.com/react-native-safeareaview/

//import React in our code
import React from 'react';

//import all the components we are going to use
import { StyleSheet, View, Text, SafeAreaView } from 'react-native';

const App = () => {
  return (
    <SafeAreaView style={{ flex: 1 }}>
      <View style={{ flex: 1, paddingHorizontal: 20 }}>
        <Text style={{ fontSize: 20 }}>
          Example of SafeArea in Recat Native
        </Text>
        <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..
        </Text>
      </View>
    </SafeAreaView>
  );
};

export default App;

To Run the React Native App

Open the terminal again and jump into your project using.

cd ProjectName

1. Start Metro Bundler

First, you will need to start Metro, the JavaScript bundler that ships with React Native. To start Metro bundler run following command:

npx react-native start

Once you start Metro Bundler it will run forever on your terminal until you close it. Let Metro Bundler run in its own terminal. Open a new terminal and run the application.

2. Start React Native Application

To run the project on an Android Virtual Device or on real debugging device:

npx react-native run-android

or on the iOS Simulator by running (macOS only)

npx react-native run-ios

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.