账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    react-lazyload加载失败,首屏渲染后,再滑动就不加载了
    • 2018-09-17 00:00
    • 11
    31
    0

    我几乎是按官方案例copy过来的,但是首屏渲染后,再滑动就不加载了,这是为什么啊,不知道是哪里属性没设置对。
    有时候又莫名其妙的,滑动一下把后面的都加载了...求救

    import React from "react";
    import LazyLoad from 'react-lazyload';
    import Lazyloading from '../Lazyloading/index';
    import Placeholder from '../Lazyloading/Placeholder';
    
    class Griddoubt extends React.Component {
        constructor(props) {
            super(props);
            this.state = {
            };
        };
        render() {
            const data = this.props.data;
            //[{once: false, id: "http://39.107.0.png", count: 1},]
            return (
                <div>
                    {data.map((el, index) => {
                        return (
                            <LazyLoad
                                once={el.once}
                                key={index}
                                height={200}
                                offset={[100, 0]}
                                placeholder={<Placeholder />} //loading
                                debounce={500}
                            >
                                <Lazyloading once={el.once} id={el.icon} count={index + 1} />
                            </LazyLoad>
                        );
                    })}
                </div>
            );
        }
    }
    
    export default Griddoubt;
    
    
    import React from "react";
    
    class Lazyloading extends React.Component {
        constructor(props) {
            super(props);
            this.state = {
                isReady: true,
                count: 1,
            };
        };
    
        componentWillReceiveProps(nextProps) {
            if (nextProps.id !== this.props.id && this.props.id) {
                this.setState({
                    isReady: false
                });
    
                setTimeout(() => {
                    this.setState({
                        isReady: true,
                        count: this.state.count + 1
                    });
                }, 500);
            } else {
                this.setState({
                    isReady: true
                });
            }
        }
    
        render() {
            console.log(this.props);//首屏渲染后,再滑动就执行不到这了
            return this.state.isReady ? (
                <div className="" style={{ height: '234PX', width: '50%', float: 'left', background: 'rgb(133, 138, 223)', textAlign: 'center', }}>
                    {this.props.once ? (
                        <div >
                            加载失败
                        </div>
                    ) : (
                            <div >
                                加载成功
                            </div>
                        )}
                </div>
            ) : (
                    <div >
                        loading...
                    </div>
                );
        }
    }
    
    export default Lazyloading;
    
    import React from 'react';
    
    export default function Placeholder() {
        return (
            <div style={{ height: '234PX', width: '50%', float: 'left', background: 'rgb(229, 142, 196)', textAlign: 'center' }}>
                <img src="http://39.107./1.png"
                    style={{ width: '50% ' }}
                />
            </div>
        );
    }
    
    0
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 11 元积分
        全部回答
    • 0
    • Infatuatedwith 普通会员 1楼

      这个问题可能是由于React Lazy Load组件的使用方式不当导致的。当React组件被首次渲染时,它会在渲染组件的同时加载数据。但是,当组件的屏幕滚动到页面底部时,如果组件还未完全加载,那么它就会停止加载。

      解决方案是在组件渲染前加载数据。这可以通过使用React的useEffect钩子来实现。在组件的useEffect钩子中,你可以初始化数据,并在组件被重新渲染时加载数据。

      以下是一个例子:

      ```jsx import React from 'react'; import { useState } from 'react';

      function MyComponent() { const [data, setData] = useState([]);

      useEffect(() => { // 在组件被重新渲染时加载数据 fetch('https://api.example.com/data') .then(response => response.json()) .then(data => setData(data)); }, []);

      return (

      {data.map(item => (
      {item.name}
      ))}
      ); } ```

      在这个例子中,我们在组件的useEffect钩子中初始化了一个数组,并在组件被重新渲染时加载了数据。这样,当组件的屏幕滚动到页面底部时,React会立即开始加载数据,而不是等待整个数据加载完成后再开始加载。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部