React - Separating DOM and Data #2059
-
Looking for guidance on #separate-dom-and-data using React. Tried filter() in the return() but then they are not included in the Lightbox. Does the thumbEl and placeholderSrc querySelector need to be rewritten? Also don’t see anything in react-photoswipe-gallery that supports this. Someone must have had this use-case before and built this..? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Your case is specifically described at #separate-dom-and-data. All you gotta do is pass the array of all images to the lightbox during the initialization and call Something like (haven't tested): import React, { useEffect } from 'react';
import PhotoSwipeLightbox from 'photoswipe/lightbox';
import 'photoswipe/style.css';
const images = [
{
src: 'https://dummyimage.com/1500x1000/555/fff/?text=Image+1',
width: 1500,
height: 1000,
},
{
src: 'https://dummyimage.com/1500x1000/555/fff/?text=Image+2',
width: 1500,
height: 1000,
},
{
src: 'https://dummyimage.com/1500x1000/555/fff/?text=Image+3',
width: 1500,
height: 1000,
},
{
src: 'https://dummyimage.com/1500x1000/555/fff/?text=Image+4',
width: 1500,
height: 1000,
}
];
export default function SimpleGallery(props) {
let lightbox;
useEffect(() => {
lightbox = new PhotoSwipeLightbox({
dataSource: images,
pswpModule: () => import('photoswipe'),
});
lightbox.init();
return () => {
if (lightbox) {
lightbox.destroy();
lightbox = null;
}
};
}, []);
return (
<>
<button
onClick={() => lightbox.loadAndOpen(2)}
type="button"
>
Open 3rd image
</button>
<button
onClick={() => lightbox.loadAndOpen(3)}
type="button"
>
Open 4th image
</button>
</>
);
} The |
Beta Was this translation helpful? Give feedback.
Your case is specifically described at #separate-dom-and-data. All you gotta do is pass the array of all images to the lightbox during the initialization and call
loadAndOpen
with the corresponding index onclick.Something like (haven't tested):