Closed newfun1994 closed 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');
@facebook-github-bot answered
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.
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]);
}