ViewWithSize

Summary: _typescript_

Reviewed By: bnelo12

Differential Revision: D16830537

fbshipit-source-id: 2ca4854a0dd4c092b6e4b09aefeb676f5921fe9e
This commit is contained in:
Daniel Büchele
2019-08-20 03:18:32 -07:00
committed by Facebook Github Bot
parent 68271e2017
commit 115e2b3576
2 changed files with 8 additions and 8 deletions

View File

@@ -0,0 +1,44 @@
/**
* Copyright 2018-present Facebook.
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
* @format
*/
import {Component} from 'react';
type ViewWithSizeProps = {
onSize: (width: number, height: number) => any;
};
type ViewWithSizeState = {
width: number;
height: number;
};
export default class ViewWithSize extends Component<
ViewWithSizeProps,
ViewWithSizeState
> {
constructor(props: ViewWithSizeProps, context: Object) {
super(props, context);
this.state = {height: window.innerHeight, width: window.innerWidth};
}
_onResize: (event: UIEvent) => void;
componentDidMount() {
this._onResize = () => {
this.setState({height: window.innerHeight, width: window.innerWidth});
};
window.addEventListener('resize', this._onResize);
}
componentWillUnmount() {
window.removeEventListener('resize', this._onResize);
}
render() {
return this.props.onSize(this.state.width, this.state.height);
}
}