react-native-orientation: Can't read getInitialOrientation of undefined

I am using react-native 0.39.2 and "react-native-orientation": "^1.17.0" My code looks like below. I will get Can't read getInitialOrientation of undefined error when executing this code. I have run link on this project.

import Orientation from 'react-native-orientation';
componentWillMount(){

    console.log('xxxx:', Orientation.getInitialOrientation());
  }

About this issue

  • Original URL
  • State: open
  • Created 7 years ago
  • Reactions: 5
  • Comments: 16

Most upvoted comments

For anyone who still is having this problem in iOS, you need to manually link the dependencies even if you ran react-native-link. To link frameworks and libraries:

Your project / the target / General -> Link Frameworks and Libraries

and copy Libraries/RCTOrientation.xcodeproj/Products/libRCTOrientaton.a into the link framework and libraries,

also you need to add this file your appdelagate.m file

#import "Orientation.h" // <--- import

@implementation AppDelegate

  // ...

  - (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    return [Orientation getOrientation];
  }

The Build Phase/Link Binary and Libraries was already linked if you ran react-native-link If not put the libRCTOrientaton.a file into the build phases as well and make sure its set to required.

Once this was done the problem was fixed for me. Hope this helps.

I rebuild the whole thing but still got undefined error. I printed the Orientation and got below output:

screen shot 2017-03-15 at 5 08 53 pm

but when I call Orientation.getInitialOrientation(), I got below error:

Possible Unhandled Promise Rejection (id: 0):
Cannot read property 'initialOrientation' of undefined
TypeError: Cannot read property 'initialOrientation' of undefined
    at Object.getInitialOrientation

This error was thrown from getInitialOrientation() method.

2017-07-24 09 46 33No libRCTOrientation. A file? I don’t have the link.

After running link you will need to rebuild the application, hot/live reloading will not pull in the newly linked native code.