简体   繁体   English

在 react 中加载后获取 Suspense 元素的高度(尺寸)

[英]Get the height (dimensions) of a Suspense element after loading in react

Basically I was trying to render a really really long list (potentially async) in React and I only want to render the visible entries±10 up and down.基本上我试图在 React 中渲染一个非常长的列表(可能是异步的),我只想向上和向下渲染可见条目±10。

I decided to get the height of the component that's holding the list, then calculate the overall list height/row height, as well as the scroll position to decide where the user have scrolled.我决定获取保存列表的组件的高度,然后计算整体列表高度/行高,以及滚动 position 来决定用户滚动的位置。

In the case below, SubWindow is a general component that could hold a list, or a picture, etc... Therefore, I decided it wasn't the best place for the calculations.在下面的例子中, SubWindow是一个通用的组件,可以保存一个列表,或者一个图片等......因此,我认为它不是计算的最佳位置。 Instead, I moved the calc to a different component and tried to use a ref instead相反,我将 calc 移动到不同的组件并尝试使用 ref 代替

const BananaWindow = (props) => {
    const contentRef = useRef(null)

    const [contentRefHeight, setContentRefHeight] = useState(0)

    useEffect(()=>setContentRefHeight(contentRef.current.offsetHeight), [contentRef])

    //calc which entries to include
    startIdx = ... 
    endIdx = ...
    ......

    return ( 
        <SubWindow 
            ref={contentRef} 
            title="all bananas" 
            content={
                <AllBananas 
                    data={props.data} 
                    startIdx={startIdx} 
                    endIdx={endIdx}
                />
            }
        />
    )
}

//this is a more general component. accepts a title and a content
const SubWindow = forwardRef((props, contentRef) => {
    return (
    <div className="listContainer">
        <div className="title">
            {props.title}
        </div>
        <div className="list" ref={contentRef}>
            {props.content}
        </div>
    </div>
})

//content for all the bananas
const AllBanana = (props) => {
    const [data, setData] = useState(null)
    
    //data could be from props.data, but also could be a get request
    if (props.data === null){
        //DATA FETCHING
        setData(fetch(props.addr).then()...)
    }

    return(
        <Suspense fallback={<div>loading...</div>}>
            //content
        </Suspense>
}

PROBLEM : In BananaWindow , the useEffect is triggered only for initial mounting and painting.问题:在BananaWindow中, useEffect仅在初始安装和绘画时触发。 So I only ended up getting the offsetWidth of the placeholder.所以我最终只得到了占位符的offsetWidth The useEffect does nothing when the content of SubWindow finishes loading.useEffect的内容完成加载时, SubWindow什么也不做。

UPDATE : Tried to use callback ref and it still only showed the height of the placeholder.更新:尝试使用回调参考,它仍然只显示占位符的高度。 Trying resize observer.尝试调整观察者的大小。 But really hope there's a simpler/out of the box way for this...但真的希望有一个更简单/开箱即用的方法......

So I solved it usingResizeObserver .所以我使用ResizeObserver解决了它。 I modified the hook from this repo to fit my project.我修改了这个 repo中的钩子以适合我的项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM