Skip to content

Instantly share code, notes, and snippets.

@almost
Created October 18, 2017 11:09
Show Gist options
  • Save almost/d166a4dc08d13dec38e63f6ac0fc3075 to your computer and use it in GitHub Desktop.
Save almost/d166a4dc08d13dec38e63f6ac0fc3075 to your computer and use it in GitHub Desktop.
// @flow
import React, { Component } from "react";
import { Dimensions } from "react-native";
type DimensionsState = { window: { width: number, height: number } };
type Props = {
children: (dimensions: DimensionsState) => React.Element<*>
};
export class WithDimensions extends Component {
state: DimensionsState;
props: Props;
constructor() {
super();
this.state = {
window: Dimensions.get("window")
};
}
_change = ({ window }: *) => {
this.setState({ window });
};
componentDidMount() {
Dimensions.addEventListener("change", this._change);
}
componentWillUnmount() {
Dimensions.removeEventListener("change", this._change);
}
render() {
return this.props.children(this.state);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment