【发布时间】:2020-05-04 17:06:58
【问题描述】:
我有以下 HOC,我考虑创建 3 个状态,这些状态将是我的断点
- 移动
- 平板电脑
- 桌面
但我想知道如何在到达断点时将这些状态设置为 true 示例:
- 手机:小于 767
- 平板电脑:768到991之间
- 桌面:992 到 1919 之间
- 大屏幕:1919 和 2520
然后当到达这些断点之一时,状态将变为 true
另外,我如何才能在我的 HOC 中只传递一个状态(以正确者为准)?
我可以改进这个逻辑吗?
import React from "react";
const withWindowResize = Component => {
class WrappedComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
height: 0,
width: 0,
mobile: false,
desktop: false,
tablet: false
};
window.addEventListener("resize", this.resizeUpdate);
}
componentDidMount() {
this.update();
}
resizeUpdate = () => {
this.setState({
height: window.innerHeight,
width: window.innerWidth
});
};
render() {
return <Component />;
}
}
return WrappedComponent;
};
export default withWindowResize;
【问题讨论】:
标签: javascript reactjs dom-events event-listener window-resize