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>
相关推荐
ZC跨境爬虫8 小时前
跟着 MDN 学 HTML day_9:(信件语义标记)
前端·css·笔记·ui·html
前端老石人9 小时前
HTML 字符引用完全指南
开发语言·前端·html
幼儿园技术家9 小时前
前端如何设计权限系统(RBAC / ABAC)?
前端
前端摸鱼匠10 小时前
Vue 3 的v-bind合并行为:讲解v-bind与普通属性合并的规则
前端·javascript·vue.js·前端框架·ecmascript
REDcker11 小时前
浏览器端Web程序性能分析与优化实战 DevTools指标与工程清单
开发语言·前端·javascript·vue·ecmascript·php·js
donecoding12 小时前
一个 sudo 引发的血案:npm 全局包权限错乱彻底修复
前端·node.js·前端工程化
风骏时光牛马12 小时前
Raku正则匹配与数据批量处理实操案例
前端
nbwenren12 小时前
2026实测:Gemini 3 镜像站视觉能力实践——拍照原型图,一键生成 HTML+CSS 代码
前端·css·html
Lee川13 小时前
Prisma 实战指南:像搭积木一样设计古诗词数据库
前端·数据库·后端