Skip to content

Instantly share code, notes, and snippets.

@corysimmons
Created February 16, 2018 10:41
Show Gist options
  • Save corysimmons/d70e38d5e795d7795686e3b7dd3a591d to your computer and use it in GitHub Desktop.
Save corysimmons/d70e38d5e795d7795686e3b7dd3a591d to your computer and use it in GitHub Desktop.
WIP React component to get dimensions of screen.
import React, { Fragment } from 'react'
import { Dimensions } from 'react-native'
export default class ScreenMediaQuery extends React.Component {
state = {
dimensions: {
width: Dimensions.get(`window`).width,
height: Dimensions.get(`window`).height,
mode: Dimensions.get(`window`).width > Dimensions.get(`window`).height ? `landscape` : `portrait`,
},
}
handleDimensionsChange = dimensions => this.setState({
dimensions: {
width: dimensions.window.width,
height: dimensions.window.height,
mode: dimensions.window.width > dimensions.window.height ? `landscape` : `portrait`,
},
})
componentWillMount () {
Dimensions.addEventListener(`change`, this.handleDimensionsChange)
}
componentWillUnmount () {
Dimensions.removeEventListener(`change`, this.handleDimensionsChange)
}
render () {
return (
<Fragment dimensions={this.state.dimensions}>
{this.props.children}
</Fragment>
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment