Get Device Orientation and Set Preferred Orientation in React Native

React Native Orientation

Hello guys, This is an Example to Get Device Orientation and Set Preferred Orientation in React Native. In this example we will see how can you control screen orientation and how can you get the rotation of the screen. Sometimes it becomes very much important to manage the orientation of the screen and if it is done properly then it will provide a user-friendly experience to your app users which is very much important for a developer.

If I have to mention the most general example of screen rotation or the orientation of the screen then a video playing application like Youtube is the best example, it uses the screen very well using the orientation as users are very comfortable with portrait mode while searching and after playing a video it provides the landscape option to use your full screen.

In this example, we are going to see how to control screen rotation/orientation using react-native-orientation library.

You can also see How to Disable Screen Rotation in React Native.

Get Device Orientation

To get the orientation of the device react-native-orientation library provides two different categories:


In the normal orientation we will get the following orientation as a response:


1. To get the Orientation any time use

Orientation.getOrientation((err, orientation) => {});

2. To add the Orientation Listener for the change in the orientation which will be triggered automatically when the orientation of the device changes

Orientation.addOrientationListener((orientation) => {});

3. To remove the Orientation Listener use


Specific Orientation

In the specific orientation we will get the following orientation as a response:


1. To get the Specific Orientation any time use

Orientation.getSpecificOrientation((err, specificOrientation) => {});

2. To add the Specific Orientation Listener for the change in the orientation which will be triggered automatically when the orientation of the device changes

Orientation.addSpecificOrientationListener((specificOrientation) => {});

3. To remove the Specific Orientation Listener use


Set Preferred Orientation

Here are the all possible orientation which you can set using this library:

1. To Lock the View to Portrait Mode


2. To Locks the View to Landscape Mode


3. To Locks the View to Right Landscape Mode


4. To Locks the View to Left Landscape Mode


5. Unlocks any Previous Locked Orientations


I think this is enough to know about the library, now let’s move towards the example. In this example, we are going to see 7 buttons on the screen in which 2 buttons will be used to get the orientation and specific orientation of the device at any time and 5 buttons to set the preferred orientation.
We have also added orientation listeners for the screen so that the current orientation of the device will be visible on the screen.
Now let’s get started with the example.

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.

Installation of Dependency

To use Orientation we have to install react-native-orientation dependency.

To install the dependencies open the terminal and jump into your project

cd ProjectName

Now install the dependency

npm install react-native-orientation --save

CocoaPods Installation

After the updation of React Native 0.60, they have introduced autolinking so we do not require to link the libraries but need to install pods.

In this example, we need to install the pods for react-native-orientation.

Please use the following command to install CocoaPods

cd ios && pod install && cd ..

Code to Get Device Orientation and Set Preferred Orientation in React Native

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


/*Example of react native orientation*/
import React, { Component } from 'react';
import {
} from 'react-native';
import Orientation from 'react-native-orientation';

export default class App extends Component {
  constructor(props) {
    this.state = {
      currentOrientation: '',
      currentSpecificOrientation: '',

  componentDidMount() {
    // The getOrientation method is async. It happens sometimes that
    // you need the orientation at the moment the JS runtime starts running on device.
    // `getInitialOrientation` returns directly because its a constant set at the
    // beginning of the JS runtime.
    const initial = Orientation.getInitialOrientation();
      currentOrientation: 'Current Device Orientation: ' + initial,

    //Listner for general orientation LANDSCAPE / PORTRAIT

    //Listner for more Specific Orientation

  _orientationChange = orientation => {
      currentOrientation: 'Current Device Orientation: ' + orientation,

  _specificOrientationChange = specificOrientation => {
        'Current Device Specific Orientation: ' + specificOrientation,

  componentWillUnmount() {
    // Remember to remove listener

  _getCurrentOrientation() {
    Orientation.getOrientation((err, orientation) => {
      alert('Orientation: ' + orientation);
  _getCurrentSpecificOrientation() {
    Orientation.getSpecificOrientation((err, specificOrientation) => {
      alert('Specific Orientation: ' + specificOrientation);

  render() {
    return (
      <View style={{ flex: 1, marginTop: 30, padding: 20 }}>
          <View style={styles.container}>
            <Text style={styles.textLarge}>
              Example of React Native Orientation
            <Text style={styles.textSmall}>
            <Text style={styles.textSmall}>
              onPress={() => this._getCurrentOrientation()}>
              <Text>get Current Orientation</Text>
              onPress={() => this._getCurrentSpecificOrientation()}>
              <Text>get Current Specific Orientation</Text>
              onPress={() => Orientation.lockToPortrait()}>
              <Text>Locks the View to Portrait Mode</Text>
              onPress={() => Orientation.lockToLandscape()}>
              <Text>Locks the View to Landscape Mode</Text>
              onPress={() => Orientation.lockToLandscapeLeft()}>
              <Text>Locks the View to Right Landscape Mode</Text>
              onPress={() => Orientation.lockToLandscapeRight()}>
              <Text>Locks the View to Left Landscape Mode</Text>
              onPress={() => Orientation.unlockAllOrientations()}>
              <Text>Unlocks any Previous Locked Orientations</Text>

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  textLarge: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  textSmall: {
    fontSize: 16,
    textAlign: 'center',
    margin: 10,
  button: {
    alignItems: 'center',
    backgroundColor: '#DDDDDD',
    padding: 10,
    width: 300,
    marginTop: 16,

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

Img   Img   Img
Img   Img   Img
Img   Img   Img

This is how you can Get Device Orientation and Set Preferred Orientation in React Native. If you have any doubts or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!

Hope you liked it. 🙂

Related Posts

5 thoughts on “Get Device Orientation and Set Preferred Orientation in React Native”

  1. Hello !
    Great article, I tried and follow it but I must have been wrong, I’ve got this error. Maybe you have an idea of where it comes from ?

    TypeError: null is not an object (evaluating ‘Orientation.initialOrientation’)
    – node_modules\react-native-orientation\index.js:98:23 in getInitialOrientation
    * App.js:27:20 in componentDidMount
    – node_modules\react-native\Libraries\Renderer\oss\ReactNativeRenderer-dev.js:15
    036:10 in commitLifeCycles
    – node_modules\react-native\Libraries\Renderer\oss\ReactNativeRenderer-dev.js:16
    636:8 in commitAllLifeCycles
    – … 21 more stack frames from framework internals

  2. Hello Snehal, great article from you.
    I am building a camera application and there i want to shoot both landscape and portrait videos,
    the problem I am facing is that I don’t want my whole screen to be rotated while the device gets rotated, instead I want my camera screen to be locked on portrait mode and rotate the icons by using “Animated”, but while locking to portrait I am not able to get the “onOrientationChange” eventListener
    so is there any way to get onOrientationChange listeners while locking to portrait?


Leave a Comment

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