ViewWithSize
Summary: _typescript_ Reviewed By: bnelo12 Differential Revision: D16830537 fbshipit-source-id: 2ca4854a0dd4c092b6e4b09aefeb676f5921fe9e
This commit is contained in:
committed by
Facebook Github Bot
parent
68271e2017
commit
115e2b3576
44
src/ui/components/ViewWithSize.tsx
Normal file
44
src/ui/components/ViewWithSize.tsx
Normal 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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user