Created
October 18, 2017 11:09
-
-
Save almost/d166a4dc08d13dec38e63f6ac0fc3075 to your computer and use it in GitHub Desktop.
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
// @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