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

React LightGallery component always create a new LightGallery instance on re-render #1416

Open
darkyzhou opened this issue Oct 18, 2022 · 3 comments

Comments

@darkyzhou
Copy link

darkyzhou commented Oct 18, 2022

Description

My LightGallery component always create a new LightGallery instance on each render.

截屏2022-10-18 21 00 53

Steps to reproduce

  1. Use Vite React template.
  2. Install the library.
  3. My codes are as below. When setList() is called, LightGallery always create a new LightGallery instance.
const LIGHT_GALLERY_PLUGINS = [lgZoom, lgRotate]
const LIGHT_GALLERY_MOBILE_SETTINGS = {
  download: true,
  controls: false,
  flipHorizontal: false,
  flipVertical: false,
  rotateRight: false,
}

function MyComponent() {
  const [list, setList] = useState([])
  return <LightGallery
    selector="[data-my-image]"
    easing="ease-in-out"
    speed={300}
    startAnimationDuration={100}
    plugins={LIGHT_GALLERY_PLUGINS}
    download={true}
    mobileSettings={LIGHT_GALLERY_MOBILE_SETTINGS}
    enableSwipe={true}
    enableDrag={true}
  >
    {list.map(item => <img key={item.key} data-my-image="" src={item.src} />)}
  </LightGallery>
}

Environment

  • Browser and version - Edge 106.0.1370.34 arm64
  • OS - macOS 12.6
  • lightGallery version - 2.6.1

Additional context

I think the bug is related to the usage of restProps:

// Lightgallery.tsx

// ...

const LG: React.FC<LightGalleryProps> = ({
    children,
    // ...
    onFlipVertical,
    ...restProps
}: LightGalleryProps) => {
    const $lg = React.useRef<HTMLDivElement>(null);

    const registerEvents = // ...

    React.useEffect(() => {
        registerEvents();
        const lightGallery = LightGallery(
            ($lg.current as unknown) as HTMLElement,
            restProps,
        );
        return function cleanup() {
            lightGallery.destroy();
        };
    // ⚠️⚠️⚠️
    // Here the reference `resetProps` ALWAYS differ on each render.
    // By temporarily removing it, the bug disappears. So a possible solution
    // might be manually do a deep check on the properties of `restProps`
    // ⚠️⚠️⚠️
    }, [registerEvents, restProps]);

    return // ...
};
export default LG;
@Poky85
Copy link

Poky85 commented Jan 17, 2023

Probably we could use something like react-fast-compare?

@Poky85
Copy link

Poky85 commented Jan 18, 2023

Change in effect dependencies has been introduced in 2.6.0, so temporary fix is downgrade to 2.5.0

@gamestap99
Copy link

same the issue. I using inline and it re-render when i update state

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

No branches or pull requests

4 participants