Created
November 16, 2017 14:23
-
-
Save SastraNababan/8480a0c27170174fb6170e79c5435424 to your computer and use it in GitHub Desktop.
React Native Dimensions Playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
import { StyleSheet, Text, View,Dimensions } from 'react-native' | |
const dim = Dimensions.get('screen') | |
export default class App extends React.Component { | |
state = { | |
screenWidth:dim.width, | |
screenHeight:dim.height, | |
orientation:this._getOrientation(dim.width,dim.height) | |
} | |
_getOrientation(width,height){ | |
if (width < height) return 'Potrait' | |
return 'Landscape' | |
} | |
_updateLayout(event){ | |
const {width,height}=event.nativeEvent.layout | |
let orientation =this._getOrientation(width,height) | |
this.setState({ | |
screenWidth:width, | |
screenHeight:height, | |
orientation:this._getOrientation(width,height) | |
}) | |
} | |
render() { | |
return ( | |
<View style={styles.container} onLayout={ (event) => this._updateLayout(event)}> | |
<Text>Orientation : {this.state.orientation}</Text> | |
<Text>Width : {this.state.screenWidth}</Text> | |
<Text>Height : {this.state.screenHeight}</Text> | |
</View> | |
) | |
} | |
} | |
const styles = StyleSheet.create({ | |
container: { | |
flex: 1, | |
backgroundColor: '#fff', | |
alignItems: 'center', | |
justifyContent: 'center', | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment