Created
February 16, 2018 10:41
-
-
Save corysimmons/d70e38d5e795d7795686e3b7dd3a591d to your computer and use it in GitHub Desktop.
WIP React component to get dimensions of screen.
This file contains hidden or 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, { 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