react-virtualized-auto-sizer中display为none子组件清空

react-virtualized-auto-sizer插件一般用来和react-window,配合使用,react-virtualized-auto-sizer负责获取,父元素的宽度和高度,传入到List中,但实战时,往往有些列表用到了隐藏显示, 比方说disabled:none;当使用disabled为none时,react-virtualized-auto-sizer会移除子元素;当返回时,元素会重新渲染,滚动条也会滚到顶部;
备注:如果是'visibility: hidden;'子元素会保留,但其会仍然占用宽度和高度,实战中很多场景不适用,这里不展开讨论;

项目中依赖
bash 复制代码
"react-virtualized-auto-sizer": "^1.0.20",
"react-window": "^1.8.9",
日常使用
typescript 复制代码
<AutoSizer>
    {({ height, width }: {height: number, width: number}) => (
        <List 
            height={height}
            itemCount={curMonitorList.length}
            itemSize={48}
            width={width}>
            {renderOrgItem}
        </List>
        )
    }
</AutoSizer>
目前未找到方案,所以重新写了一个自定义组件完成AutoSizer要办的事
typescript 复制代码
import { useEffect, useState, useRef } from "react";
interface PropsType{
    children: any
}
export default function AzAutoSizer({children}: PropsType) {
    const curRef = useRef<HTMLDivElement>(null)
    const [curWidthHeight, setCurWidthHeight] = useState({
        height: 0,
        width: 0,
    });
    const handleClick = () => {
        if (curRef.current) {
            const parentNode: any = curRef.current.parentNode;
            const height = parentNode.offsetHeight;
            const width = parentNode.offsetWidth;
            curWidthHeight.height = height || 0;
            curWidthHeight.width = width || 0;
            setCurWidthHeight({...curWidthHeight});
        }
    };
    useEffect(() => {
        handleClick();
        const handleResize = () => {
            /*   如果示父或祖先disabled: none时,其父高度宽度都为0;
                if (!curRef.current) { return; }
                const parentNode: any = curRef.current.parentNode;
                const height = parentNode.offsetHeight
                const width = parentNode.offsetWidth;
                // 表示父或祖先disabled: none;
                if (height === 0 && width === 0) {
                    return;
                }
            */
            handleClick();
        };
        /* 监听窗口改变 */
        window.addEventListener('resize', handleResize);
        /* 是否进入可视化-1.创建观察函数 */
        const observer = new IntersectionObserver(([{ isIntersecting }]) => {
            if (isIntersecting) {
                if (!curRef.current) { return; }
                if (curWidthHeight.height !== 0 && curWidthHeight.width !== 0) {
                    /* 如果这段代码放出来的话要解开resize那边的代码判断,才能满足父或祖先元素disabled:none时组件宽高被重置为none
                    */
                    // observer.unobserve(curRef.current);
                } else {
                    handleClick();
                }
            }
        });
        /* 是否进入可视化-1.监听当前元素 */
        curRef.current && observer.observe(curRef.current);
        
        return () => {
            /* 卸载窗口监听 */
            window.removeEventListener('resize', handleResize);
            /* 卸载可视化监听 */
            if (curRef.current) {
                observer.unobserve(curRef.current);
            }
        };
    }, []);
    
    return <div ref={curRef} style={{width: `${curWidthHeight.width}px`, height: `${ curWidthHeight.height}px`}}>
        {children({height: curWidthHeight.height, width: curWidthHeight.width})}
    </div>
}
使用方法
typescript 复制代码
<AzAutoSizer >
    {({ height, width }: {height: number, width: number}) => (
        <List 
            height={height}
            itemCount={curMonitorList.length}
            itemSize={48}
            width={width}>
            {renderOrgItem}
        </List>
        )
    }
</AzAutoSizer>
相关推荐
拳打南山敬老院3 分钟前
Context 不是压缩出来的,而是设计出来的
前端·后端·aigc
用户3076752811277 分钟前
💡 从"傻等"到"流淌":我在AI项目中实现流式输出的血泪史(附真实代码+深度解析)
前端
bluceli8 分钟前
前端性能优化实战指南:让你的网页飞起来
前端·性能优化
SuperEugene10 分钟前
Vue状态管理扫盲篇:如何设计一个合理的全局状态树 | 用户、权限、字典、布局配置
前端·vue.js·面试
没想好d11 分钟前
通用管理后台组件库-9-高级表格组件
前端
阿虎儿14 分钟前
React Hook 入门指南
前端·react.js
核以解忧38 分钟前
借助VTable Skill实现10W+数据渲染
前端
WangHappy40 分钟前
不写 Canvas 也能搞定!小程序图片导出的 WebView 通信方案
前端·微信小程序
李剑一44 分钟前
要闹哪样?又出现了一款新的格式化插件,尤雨溪力荐,速度提升了惊人的45倍!
前端·vue.js
闲云一鹤1 小时前
Git LFS 扫盲教程 - 你不会还在用 Git 管理大文件吧?
前端·git·前端工程化