React-native-orientation: Orientation.lockToPortrait() throw a error

Created on 12 Jan 2017  Â·  8Comments  Â·  Source: yamill/react-native-orientation

I was write it at componentDidMount(), but it throw a red error like this:
undefined is not an object(evaluating ‘Orientation.lockToportrait’)

Most helpful comment

You most likely don't have the latest version, when I did npm install, it didn't actually get the latest files (it downloaded the version that used OrientationPackage(this)), so I had the same error. The correct version should have OrientationPackage() in MainApplication.

Here's what I did to fix it and get it working.

Try to remove the react-native-orientation from your project with:

npm remove react-native-orientation

Then download the zip, rename it to react-native-orientation and copy/move it to your npm_modules folder. Then link it again.

react-native link react-native-orientation

Note you may have to remove all references of react-native-orientation from the Android project first before you can re-link it.

All 8 comments

I'm having a very similar issue on Android.
Cannot read property of 'lockToLandscapeLeft' of undefined.

import Orientation from 'react-native-orientation'
...
componentDidMount () {
  Orientation.lockToLandscapeLeft()
}

putting in a console.log on Orientation produces:

addOrientationListener: addOrientationListener(cb)
addSpecificOrientationListener: addSpecificOrientationListener(cb)
getInitialOrientation: getInitialOrientation()
getOrientation: getOrientation(cb)
getSpecificOrientation: getSpecificOrientation(cb)
lockToLandscape: lockToLandscape()
lockToLandscapeLeft: lockToLandscapeLeft()
lockToLandscapeRight: lockToLandscapeRight()
lockToPortrait: lockToPortrait()
removeOrientationListener: removeOrientationListener(cb)
removeSpecificOrientationListener: removeSpecificOrientationListener(cb)
unlockAllOrientations: unlockAllOrientations()
__proto__: Object

so the object is clearly loaded, but the function call is failing

Im also having the same issue as @ostigley

sup @puresamari . I'm not sure @yamill has much free time to support this anymore. But after doing some reading, I figured out you you can make it work with the following:

var Orientation = require('react-native').NativeModules.Orientation
...
Orientation. lockToLandscapeLeft()

and it worked. Winner Winner Chicken Dinner

Tried both options,

  1. import Orientation from 'react-native-orientation';
  2. As suggested by @ostigley , var Orientation = require('react-native').NativeModules.Orientation

But still gives error undefined is not an object(evaluating ‘Orientation.lockToportrait’)

You most likely don't have the latest version, when I did npm install, it didn't actually get the latest files (it downloaded the version that used OrientationPackage(this)), so I had the same error. The correct version should have OrientationPackage() in MainApplication.

Here's what I did to fix it and get it working.

Try to remove the react-native-orientation from your project with:

npm remove react-native-orientation

Then download the zip, rename it to react-native-orientation and copy/move it to your npm_modules folder. Then link it again.

react-native link react-native-orientation

Note you may have to remove all references of react-native-orientation from the Android project first before you can re-link it.

Just fork this repository and use npm install yourgitlogin/react-native-orientation --save to install the package. It works for me.

@danilvalov could you explain me why did it work?

@andfs the current version of the React Native Orientation package in NPM is not up to date. For now, just fetch the code directly from the repo like:

npm install --save react-native-orientation@git+https://github.com/yamill/react-native-orientation.git

The current version at master should have lockToPortrait() working on Android and iOS granted native linking/installing is correct.

If error persist, please open another issue with an example/codebase for us to look at and debug.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sahas- picture sahas-  Â·  7Comments

testxin picture testxin  Â·  4Comments

wellyshen picture wellyshen  Â·  3Comments

sergio-solnet picture sergio-solnet  Â·  9Comments

MrEdinLaw picture MrEdinLaw  Â·  3Comments