Skip to content

Instantly share code, notes, and snippets.

@basarat
Created October 13, 2016 06:54
Show Gist options
  • Save basarat/eb0d6aaeb88d73f409307e5d35006376 to your computer and use it in GitHub Desktop.
Save basarat/eb0d6aaeb88d73f409307e5d35006376 to your computer and use it in GitHub Desktop.
Just and idea on window size measurement
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