banthagroup / fslightbox-react

Basic version of Fullscreen Lightbox for React.js. Website: https://fslightbox.com/react
MIT License
168 stars 23 forks source link

Lightbox close toggler state not properly updating #232

Open exitsimulation opened 1 year ago

exitsimulation commented 1 year ago

Hello!

What's the intended behaviour when closing the lightbox? I thought fslightbox would take care about the toggler state on close, however this seems not to be the case? I then tried to flipping the toggle state in the onClose callback, however, this leads to the lightbox opening again immediately and only after a second close the lightbox closes successfully.

What is the best way to implement the closing behaviour properly? Thanks


const [lightboxOpen, setLightboxOpen] = useState(false);

[...]

{image_slider && imagesLightbox && (
          <FsLightbox
            toggler={lightboxOpen}
            sources={imagesLightbox}
            onClose={() => {
              setLightboxOpen(false);
            }}
          />
        )}```
MariuszRogowicz commented 1 year ago

same problem here - The toggler does not update the shutdown state on its own, but only the change. When we try to override this with the onClose event, the component rerender again.

exitsimulation commented 1 year ago

@banthagroup Any info on what would be the best approach here? Thanks

deepkmal commented 1 year ago

I'm running into the same problem as well - any info on how to proceed would be greatly appreciated

PapaLimaZulu commented 1 year ago

The workaround:

In setUpLightboxUpdater (1/c/main-component/updating)

replace

if (previousProps.toggler !== currentProps.toggler) {

with

if (currentProps.toggler && previousProps.toggler !== currentProps.toggler) {

27leaves commented 1 year ago

I got it (for all who are as confused as I am). This toggle state is really a toggle and not a "is visible" flag. So, everytime you change the toggler it will open the view. That's why onClose={() => {setLightboxOpen(false); }} will open the view again and seems broken. It's a bit of a confusing API, but as soon as you got it it works.

So the solution is not to do anything onClose, but have it like this:

const [lightboxToggle, setLightboxToggle] = useState(false);

[...]
const openLightbox = () => {
   setLightboxToggle(!lightboxToggle);
}

{image_slider && imagesLightbox && (
          <FsLightbox
            toggler={lightboxToggle}
            sources={imagesLightbox}
            //onClose={() => {
            //  setLightboxOpen(false);
            //}}
          />
        )}
deshabhishek007 commented 9 months ago

Thanks @27leaves it worked.

I got it (for all who are as confused as I am). This toggle state is really a toggle and not a "is visible" flag. So, everytime you change the toggler it will open the view. That's why onClose={() => {setLightboxOpen(false); }} will open the view again and seems broken. It's a bit of a confusing API, but as soon as you got it it works.

So the solution is not to do anything onClose, but have it like this:

const [lightboxToggle, setLightboxToggle] = useState(false);

[...]
const openLightbox = () => {
   setLightboxToggle(!lightboxToggle);
}

{image_slider && imagesLightbox && (
          <FsLightbox
            toggler={lightboxToggle}
            sources={imagesLightbox}
            //onClose={() => {
            //  setLightboxOpen(false);
            //}}
          />
        )}
Mrrvm commented 7 months ago

Hello!

I have a similar problem, I have implemented exactly what is here and works, but when I try to open the lightbox for the second time, it won't work anymore, because the toggler value does not get updated. Any ideas? Thank you.