Skip to content

Gallery items ref is not removing unmounted items correctlyΒ #1470

@TilWessel

Description

@TilWessel

Hi! πŸ‘‹

Firstly, thanks for your work on this project! πŸ™‚

I am using the Gallery component outside of a virtualized table with cells that include an Item component. When scrolling away such an Item, which results in unmounting of the row becuase of virtualization, the Gallery should remove it from its items ref map. Because this is not happening, when scrolling back up to the row with that Item, trying to open it will result in an error from ensureRefPassed.

I used patch-package to fix it.

Here is the diff that solved my problem:

diff --git a/node_modules/react-photoswipe-gallery/dist/gallery.js b/node_modules/react-photoswipe-gallery/dist/gallery.js
index 04bd441..fce6c69 100644
--- a/node_modules/react-photoswipe-gallery/dist/gallery.js
+++ b/node_modules/react-photoswipe-gallery/dist/gallery.js
@@ -284,6 +284,10 @@ export const Gallery = ({
     items.current.delete(ref);
   }, []);
   const set = useCallback((ref, data) => {
+    if (ref.current === null) {
+        return;
+    }
+
     items.current.set(ref, data);
     if (openWhenReadyPid.current === null) {
       return;
diff --git a/node_modules/react-photoswipe-gallery/dist/item.js b/node_modules/react-photoswipe-gallery/dist/item.js
index 344d904..3db2a60 100644
--- a/node_modules/react-photoswipe-gallery/dist/item.js
+++ b/node_modules/react-photoswipe-gallery/dist/item.js
@@ -45,9 +45,7 @@ export const Item = _a => {
   }), [refCallback, open, close]);
   useEffect(() => {
     return () => {
-      if (ref.current === null) {
-        remove(ref);
-      }
+      remove(ref);
     };
   }, [remove]);
   return children(childrenFnProps);

This issue body was partially generated by patch-package.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions