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>
相关推荐
海鸥两三2 小时前
uniapp 小程序引入 uview plus 框架,获得精美的UI框架
前端·vue.js·ui·小程序·uni-app
lightgis3 小时前
16openlayers加载COG(云优化Geotiff)
前端·javascript·html·html5
小飞大王6663 小时前
TypeScript核心类型系统完全指南
前端·javascript·typescript
你的人类朋友5 小时前
✍️记录自己的git分支管理实践
前端·git·后端
合作小小程序员小小店5 小时前
web网页开发,在线考勤管理系统,基于Idea,html,css,vue,java,springboot,mysql
java·前端·vue.js·后端·intellij-idea·springboot
防火墙在线5 小时前
前后端通信加解密(Web Crypto API )
前端·vue.js·网络协议·node.js·express
Jacky-0085 小时前
Node + vite + React 创建项目
前端·react.js·前端框架
CoderYanger6 小时前
前端基础——CSS练习项目:百度热榜实现
开发语言·前端·css·百度·html·1024程序员节
i_am_a_div_日积月累_6 小时前
10个css更新
前端·css
倚栏听风雨7 小时前
npm命令详解
前端