I was write it at componentDidMount(), but it throw a red error like this:
undefined is not an object(evaluating ‘Orientation.lockToportrait’)
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,
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.
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-orientationThen 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-orientationNote you may have to remove all references of react-native-orientation from the Android project first before you can re-link it.