Created
June 12, 2019 21:08
-
-
Save robinheinze/44052694e5045473a7902edfe93f94bb to your computer and use it in GitHub Desktop.
Using react-native Dimensions API dynamically
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 * as React from "react" | |
import { View, Dimensions } from 'react-native' | |
const STYLE = { | |
width: Dimensions.get("window").width, | |
height: Dimensions.get("window").height, | |
backgroundColor: "yellow" | |
} | |
export class MyComponent extends React.Component { | |
render() { | |
return ( | |
<View style={STYLE}> | |
<Text>"My View"<Text/> | |
</View> | |
} | |
} | |
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 * as React from "react" | |
import { View, Dimensions } from 'react-native' | |
const STYLE = { | |
backgroundColor: "yellow" | |
} | |
const getScreenWidth = () => Dimensions.get("window").width | |
const getScreenHeight = () => Dimensions.get("window").height | |
export class MyComponent extends React.Component { | |
render() { | |
const dimensions = { | |
width: getScreenWidth(), | |
height: getScreenHeight() | |
} | |
return ( | |
<View style={{ ...STYLE, ...dimensions }}> | |
<Text>"My View"<Text/> | |
</View> | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice!