【问题标题】:How can I set react-lazyload placeholder prop to none when it reaches the end of an array?当它到达数组末尾时,如何将 react-lazyload 占位符属性设置为无?
【发布时间】:2020-11-02 04:17:12
【问题描述】:

我正在为我的 react js 项目使用 react-lazyload 库。

import LazyLoad from 'react-lazyload';

我首先使用 useEffect 获取数据:

const [ stuff, setStuff ] = React.useState(null);
    React.useEffect(() => {
        Axios.get('https://jsonplaceholder.typicode.com/posts')
            .then((res) => setStuff(res.data))
            .catch((err) => console.log(err.message));
    }, []);

然后我用延迟加载显示 stuff 数组,它工作得非常好:

    {stuff ? (
                stuff.map((each) => {
                    return (
                            <LazyLoad
                                offset={[ -100, 100 ]}
                                placeholder={<h2>loading...</h2>}
                                key={each.id}
                            >
                                <h3 style={{ margin: 20 }}>{each.title}</h3>
                                <LazyLoad once={true} placeholder={`https://picsum.photos/id/${each.id}/5/5`}>
                                    <img
                                        data-aos-duration="2000"
                                        data-aos="fade"
                                        src={`https://picsum.photos/id/${each.id}/200/200`}
                                    />
                                </LazyLoad>
                            </LazyLoad>
                    );
                })
            ) : null}

所以首先它显示每个项目的占位符道具

loading...

并通过向下滚动显示实际项目。

唯一的问题是它不知道项目何时完成并且没有要显示的项目,因此当您到达屏幕末尾并且项目已完成时,它仍然显示

加载中...

如何知道 stuff 数组的长度,使其在到达数组末尾时不显示正在加载的东西?

【问题讨论】:

    标签: reactjs lazy-loading react-lazy-load


    【解决方案1】:

    我通过在标签中添加 100px 的高度和宽度来解决此问题,其中包含加载文本。

    【讨论】:

      猜你喜欢
      • 2015-03-13
      • 1970-01-01
      • 2023-04-08
      • 2018-03-08
      • 1970-01-01
      • 2018-06-15
      • 2017-10-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多