Skip to content

Instantly share code, notes, and snippets.

@robinheinze
Created June 12, 2019 21:08
Show Gist options
  • Save robinheinze/44052694e5045473a7902edfe93f94bb to your computer and use it in GitHub Desktop.
Save robinheinze/44052694e5045473a7902edfe93f94bb to your computer and use it in GitHub Desktop.
Using react-native Dimensions API dynamically
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>
}
}
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>
}
}
@craftdelivery
Copy link

Nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment