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

[Feature request] Support for multiple modals/galleries on a page #380

Open
jpkelly opened this issue Jun 2, 2020 · 2 comments
Open

[Feature request] Support for multiple modals/galleries on a page #380

jpkelly opened this issue Jun 2, 2020 · 2 comments

Comments

@jpkelly
Copy link

jpkelly commented Jun 2, 2020

When adding 2 or more modals/carousels on a page there is a conflict. Can the instances (states?) have a property where they can be assigned a unique identifier?

Expected: When clicking a thumbnail for one of two or more galleries the image the image appearing in the lightbox should be the same as the image in thumbnail.

Actual: The image which appears is from the last gallery on the page regardless of which gallery is clicked on.

Example:
https://codesandbox.io/s/react-photo-gallery-with-image-viewer-gcsq8

@jpkelly
Copy link
Author

jpkelly commented Jun 2, 2020

I am very new to React. I would attempt to implement this but not sure where to start.

@jpkelly jpkelly changed the title Support for multiple modals/galleries on a page [Feature request] Support for multiple modals/galleries on a page Jun 2, 2020
@BrendanHart
Copy link
Contributor

I believe your issue is to do with a misuse of state.

You are using the same closeLightbox and openLightbox callbacks for each Modal, and therefore you will see when you click an image, both of the images are displayed, with the second lightbox image being on top of the image selected from the first lightbox. The images that displayed have the same ID in their respective lightbox, since we are using the same state.

To fix this, you need to have a separate

  const [currentImage, setCurrentImage] = useState(0);
  const [viewerIsOpen, setViewerIsOpen] = useState(false);

for each Modal, and then create separate openLightbox and closeLightbox callbacks for each.

Hope this helps.

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