javascript - 检查组件是否在视口中:无限渲染
问题描述
我想检查特定组件是否在视口中,它似乎正在工作,但该组件正在无限渲染。
componentDidMount() {
document.addEventListener('scroll', this.checkViewPort,true);
}
componentWillUnmount() {
document.removeEventListener('scroll', this.checkViewPort);
}
checkViewPort = () => {
const rect = this.scrollRef.getBoundingClientRect();
const isInview = ( rect.bottom < 0 || rect.right < 0 || rect.left > window.innerWidth ||
(rect.top) > window.innerHeight);
this.setState({hintViewPort:!isInview});
}
setWrapperRef = (node) => {
this.scrollRef = node;
};
<div ref = {this.setWrapper}> </div>
解决方案
推荐阅读
- python - 删除模型保存时的 tensorflow.python.framework.ops.Tensor
- python - 列表中的表格数据
- java - 如何根据列值 Java 将 2D 数组拆分为多个 2D 数组?
- grep - grep 或 ripgrep:如何仅查找与多个模式匹配的文件(不仅在同一行上)?
- swift - Swift:将通用协议作为参数的通用函数不起作用
- jenkins - 在 Jenkins 中多次构建失败时锁定 TFS 签入
- postman - 如何在 Postman 请求的 json 正文中将环境变量插入为数字?
- python - 如何在 Python Gekko 中设置求解器选项(例如容错)?
- powershell - Powershell Script将文件从昨天复制到其他文件夹
- python - 在 django 项目的生产模式下访问文件