Skip to content

Instantly share code, notes, and snippets.

@romchambe
Created March 18, 2019 16:26
Show Gist options
  • Save romchambe/c92b8a8ff6a2840ff9b75efad1517505 to your computer and use it in GitHub Desktop.
Save romchambe/c92b8a8ff6a2840ff9b75efad1517505 to your computer and use it in GitHub Desktop.
export class MainLayout extends React.Component {
dimensionsUpdatePoll: any = null;
constructor(props){
super(props);
this.state = {
width: 0,
height: 0
}
this.lastCheck = Date.now()
this.dimensionsUpdateChecker = this.dimensionsUpdateChecker.bind(this)
this.dimensionsUpdatePoll = setInterval(
() => this.dimensionsUpdateChecker(Dimensions.get("window").width, Dimensions.get("window").height)
, 500)
}
componentDidMount(){
Dimensions.addEventListener('change',
dimensions => this.dimensionsUpdateChecker(dimensions.window.width, dimensions.window.height)
)
}
componentWillUnmount(){
clearInterval(this.dimensionsUpdatePoll)
Dimensions.removeEventListener('change', ()=>{})
}
dimensionsUpdateChecker(currentWidth, currentHeight){
let { width, height } = this.state
if (Date.now() - this.lastCheck > 250 && (currentWidth !== width || currentHeight !== height)) {
this.setState({
width: currentWidth, height: currentHeight
})
}
this.lastCheck = Date.now()
}
render(){
return(<View />)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment