facebook / react-native

A framework for building native applications using React
https://reactnative.dev
MIT License
118.14k stars 24.21k forks source link

iOS - when debug in chrome Cannot read property 'screenWidth' of undefined #5996

Closed newfun1994 closed 8 years ago

newfun1994 commented 8 years ago

No any problem when run react native iOS app without debugging in chrome. but when debug in chrome Cannot read property 'screenWidth' of undefined. I use callback function to get screenWidth from objective-c

index.ios.js var DeviceInfo = require('react-native').NativeModules.DeviceInfo; var deviceInfo;

DeviceInfo.deviceInfoEvent((error, events) => { if (error) { console.error(error); } else { deviceInfo = events; console.log({deviceInfo}); } });`

class ReactNativeDemo extends Component { render() { return ( <View style={styles.container}> <Text> screenWidth:{deviceInfo.screenWidth}{'\n'} </Text> </View> ); } }

DeviceInfo.m RCT_EXPORT_METHOD(deviceInfoEvent:(RCTResponseSenderBlock)callback){ NSNumber *screenWidth = @([UIScreen mainScreen].bounds.size.width); NSDictionary *deviceInfo = @{ @"screenWidth":screenWidth}; callback(@[[NSNull null], deviceInfo]); }

cosmith commented 8 years ago

You shouldn't have to use DeviceInfo to get screen width, use Dimensions instead, like this:

var {Dimensions} = require("react-native");
var {height, width} = Dimensions.get('window');
cosmith commented 8 years ago

@facebook-github-bot answered

facebook-github-bot commented 8 years ago

Closing this issue as @cosmith says the question asked has been answered. Please help us by asking questions on StackOverflow. StackOverflow is amazing for Q&A: it has a reputation system, voting, the ability to mark a question as answered. Because of the reputation system it is likely the community will see and answer your question there. This also helps us use the GitHub bug tracker for bugs only.