Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The first component loads more than once. #411

Open
RezaGhx opened this issue Aug 9, 2023 · 1 comment
Open

The first component loads more than once. #411

RezaGhx opened this issue Aug 9, 2023 · 1 comment

Comments

@RezaGhx
Copy link

RezaGhx commented Aug 9, 2023

the first component which has been loaded on the first view, loads again after scrolling.

the image attached on the task, describes the problem.
after the page mounts, the first avatar, which marked with a red one, loads first of all but when we scroll down the page, it loads again.

but the other avatars like the second, and the third specified on the attachment, loads only on their visible condition and it's a correct behavior.

this issue causes at least 2x requests on the server and seems harmful on the project performance.

the image attached is from the Google Chrome devTools, where you can see the request called twice; however the first avatar's request called about 4 or 5 times according to our Mozilla Firefox observations.

here is a snippet of simple usage:

snippet

`import { Image } from 'antd';
import LazyLoad from 'react-lazyload';

const Avatar = ({ src }) => {
return (

<Image height={85} width={85} alt={'lorem'} src={src ? src + '?' + new Date().getTime() : '/img/user.png'} />

);
};`

also I found a relatable issue on the package issues but unfortunately it has been closed without any answers or comments.
#203

react_lazyload_issue

@kyoungholee
Copy link

kyoungholee commented May 22, 2024

  1. Remove the timestamp

import { Image } from 'antd';
import LazyLoad from 'react-lazyload';

const Avatar = ({ src }) => {
return (

<LazyLoad>
  <Image height={85} width={85} src={src ? src : '/img/user.png'} />
</LazyLoad>

);
};

export default Avatar;

  1. Fixed a timestamp

import React, { useState, useEffect } from 'react';
import { Image } from 'antd';
import LazyLoad from 'react-lazyload';

const Avatar = ({ src }) => {
const [imageSrc, setImageSrc] = useState(src ? ${src}?${new Date().getTime()} : '/img/user.png');

useEffect(() => {
if (src) {
setImageSrc(${src}?${new Date().getTime()});
} else {
setImageSrc('/img/user.png');
}
}, [src]);

return (

<LazyLoad>
  <Image height={85} width={85} src={imageSrc} />
</LazyLoad>

);
};

export default Avatar;

  1. Component Optimization

import React, { useState, useEffect } from 'react';
import { Image } from 'antd';
import LazyLoad from 'react-lazyload';

const Avatar = React.memo(({ src }) => {
const [imageSrc, setImageSrc] = useState(src ? ${src}?${new Date().getTime()} : '/img/user.png');

useEffect(() => {
if (src) {
setImageSrc(${src}?${new Date().getTime()});
} else {
setImageSrc('/img/user.png');
}
}, [src]);

return (

<LazyLoad>
  <Image height={85} width={85} src={imageSrc} />
</LazyLoad>

);
});

export default Avatar;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants