import React, { useState, useEffect, useRef, useCallback } from 'react'; import LightGallery from 'lightgallery/react'; import lgZoom from 'lightgallery/plugins/zoom'; import lgHash from 'lightgallery/plugins/hash'; import 'lightgallery/css/lightgallery.css'; const LcpGallery = (props) => { const [settings, setSettings] = useState({ elementClassNames: 'lcp-gallery grid', plugins: [], zoom: false, hash: false, download: false }); const lightGallery = useRef(null); // onInit callback to initialize the lightGallery instance const onInit = useCallback((detail) => { if (detail) { lightGallery.current = detail.instance; } }, []); // Update settings based on the props useEffect(() => { console.log("Props received:", props); const plugins = []; // Convert plugin strings to actual plugin functions if (props.plugins) { props.plugins.forEach((plugin) => { console.log(`Adding plugin: ${plugin}`); if (plugin === 'lgZoom') { plugins.push(lgZoom); // Add lgZoom function } else if (plugin === 'lgHash') { plugins.push(lgHash); // Add lgHash function } }); } // Update settings with new plugins and options setSettings((prevSettings) => ({ ...prevSettings, plugins, // Updated plugins zoom: props.zoom || false, // Default zoom option hash: props.hash || false, // Default hash option download: props.download || false })); }, [props]); // Reinitialize gallery when settings change (with careful timing) useEffect(() => { console.log("Settings changed:", settings); // Check if gallery instance exists and only refresh if initialized if (lightGallery.current) { console.log("Refreshing gallery..."); lightGallery.current.refresh(); // Refresh the gallery instance } }, [settings]); return ( {/* Gallery Items */} layers of blue. ); }; export default LcpGallery;