Created
October 13, 2016 06:54
-
-
Save basarat/eb0d6aaeb88d73f409307e5d35006376 to your computer and use it in GitHub Desktop.
Just and idea on window size measurement
This file contains 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 * as onresize from 'onresize'; | |
export type WindowDetails = { | |
width: number, | |
height: number | |
} | |
/** | |
* Doesn't render anything itself. Instead calls a function with the details. | |
* Warning: will not work for server side rendering | |
*/ | |
export class WindowResponsive extends React.Component<{ render: (details: WindowDetails) => JSX.Element }, WindowDetails>{ | |
dispose = () => null; | |
loadWindowDetails = () => { | |
this.setState({ | |
width: window.innerWidth, | |
height: window.innerHeight | |
}); | |
} | |
componentWillMount() { | |
this.loadWindowDetails(); | |
this.dispose = onresize.on(() => { | |
this.loadWindowDetails(); | |
}).dispose; | |
} | |
componentWillUnmount() { | |
this.dispose(); | |
} | |
render() { | |
return this.props.render(this.state); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment