react-native-orientation icon indicating copy to clipboard operation
react-native-orientation copied to clipboard

Can't read getInitialOrientation of undefined

Open joeyzhao0113 opened this issue 7 years ago • 16 comments

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());
  }

joeyzhao0113 avatar Mar 12 '17 07:03 joeyzhao0113

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

le6o avatar Mar 14 '17 12:03 le6o

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.

joeyzhao0113 avatar Mar 15 '17 06:03 joeyzhao0113

save problem

mayaa6 avatar May 01 '17 03:05 mayaa6

I'm having the same problem, but only on ios. My android version works fine.

xilonxiao avatar May 08 '17 01:05 xilonxiao

@joeyzhao0113 @mayaa6 did you guys manage to fix this?

bharath-kamath avatar May 18 '17 13:05 bharath-kamath

not yet.

On Thu, May 18, 2017 at 9:09 PM, bharath-kamath [email protected] wrote:

@joeyzhao0113 https://github.com/joeyzhao0113 @mayaa6 https://github.com/mayaa6 did you guys manage to fix this?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/yamill/react-native-orientation/issues/160#issuecomment-302398559, or mute the thread https://github.com/notifications/unsubscribe-auth/ADEmotiWI1K4cX32bTNVHmoGLHe6i9ONks5r7EMDgaJpZM4MaeJw .

mayaa6 avatar May 19 '17 01:05 mayaa6

I don’t have plan on that.

On 18 May 2017, at 11:09 pm, bharath-kamath [email protected] wrote:

@joeyzhao0113 https://github.com/joeyzhao0113 @mayaa6 https://github.com/mayaa6 did you guys manage to fix this?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/yamill/react-native-orientation/issues/160#issuecomment-302398559, or mute the thread https://github.com/notifications/unsubscribe-auth/AWdMip4H_am-gNTiABJ1N0K8GU00V-rFks5r7EMDgaJpZM4MaeJw.

joeyzhao0113 avatar May 19 '17 01:05 joeyzhao0113

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.

Visylvius avatar Jul 14 '17 10:07 Visylvius

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

I am not familiar with iOS project, and how can I found the file Libraries/RCTOrientation.xcodeproj/Products/libRCTOrientaton.a?

mayaa6 avatar Jul 15 '17 10:07 mayaa6

If you go into xcode, right beneath the x in upper left hand corner there are 8 icons. Click on the folder icon then click on your project. In your project follow the path Libraries/RCTOrientation.xcodeproj/Products/libRCTOrientaton.a

Visylvius avatar Jul 17 '17 09:07 Visylvius

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

PuXinLin avatar Jul 24 '17 01:07 PuXinLin

@1551715165 the file in red color image

mayaa6 avatar Jul 24 '17 02:07 mayaa6

@mayaa6 I know. I don't know this file in the whole project all find it again. Don't have this. A file. This file is created by yourself?

PuXinLin avatar Jul 24 '17 03:07 PuXinLin

#import "../../node_modules/react-native-orientation/iOS/RCTOrientation/Orientation.h" or $(SRCROOT)/../node_modules/react-native-orientation/iOS/RCTOrientation (make sure it is recursive)

jpcarts23 avatar Sep 19 '18 07:09 jpcarts23

Tried what @jpcarts23 and @visylvius mentioned and it works on android and ios. But for ios it doesn't work when trying to run the simulator through react-native-cli in the command line. Works perfectly fine in xCode. Does anyone know why? Getting the error Cannot read property '...' of undefined

devonhuangcodes avatar Dec 11 '18 17:12 devonhuangcodes

Same error. I can understand ios link steps but react-native link works well with ios (not with Android that I linked myself). If I run with xcode work in ios too...

icastillejogomez avatar Dec 23 '18 00:12 icastillejogomez