Created
March 18, 2019 16:26
-
-
Save romchambe/c92b8a8ff6a2840ff9b75efad1517505 to your computer and use it in GitHub Desktop.
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
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