React 监听屏幕变化,控制组件高度
import React from "react";
class Index extends React.Component {
constructor(props) {
super(props);
this.state = {
clientHeight: document.documentElement.clientHeight,
};
this.resize = this.resize.bind(this);
}
componentDidMount() {
window.addEventListener("resize", this.resize);
}
componentWillUnmount() {
window.removeEventListener("resize", this.resize);
}
resize() {
this.setState({ clientHeight: document.documentElement.clientHeight });
}
render() {
return (
<h1>{this.state.clientHeight}</h1>
);
}
}
export default Index;