unpkg/client/WindowSize.js

35 lines
705 B
JavaScript
Raw Normal View History

2018-02-18 02:00:56 +00:00
import React from "react";
import PropTypes from "prop-types";
import addEvent from "./utils/addEvent";
import removeEvent from "./utils/removeEvent";
2018-02-18 02:00:56 +00:00
const ResizeEvent = "resize";
class WindowSize extends React.Component {
static propTypes = {
onChange: PropTypes.func
2018-02-18 02:00:56 +00:00
};
handleWindowResize = () => {
if (this.props.onChange)
this.props.onChange({
width: window.innerWidth,
height: window.innerHeight
2018-02-18 02:00:56 +00:00
});
};
componentDidMount() {
2018-02-18 02:00:56 +00:00
addEvent(window, ResizeEvent, this.handleWindowResize);
}
componentWillUnmount() {
2018-02-18 02:00:56 +00:00
removeEvent(window, ResizeEvent, this.handleWindowResize);
}
render() {
2018-02-18 02:00:56 +00:00
return null;
}
}
2018-02-18 02:00:56 +00:00
export default WindowSize;