ImageWithFallback: change image when src change
This commit is contained in:
@@ -46,7 +46,7 @@ const ImageWithFallback = ({
|
|||||||
{
|
{
|
||||||
rootMargin: "50px", // Start loading 50px before the image comes into view
|
rootMargin: "50px", // Start loading 50px before the image comes into view
|
||||||
threshold: 0.1,
|
threshold: 0.1,
|
||||||
}
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
if (imageRef.current) {
|
if (imageRef.current) {
|
||||||
@@ -70,6 +70,13 @@ const ImageWithFallback = ({
|
|||||||
setHasError(false);
|
setHasError(false);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
// Update imgSrc when src prop changes
|
||||||
|
useEffect(() => {
|
||||||
|
setImgSrc(src || fallbackSrc);
|
||||||
|
setIsLoading(true);
|
||||||
|
setHasError(false);
|
||||||
|
}, [src, fallbackSrc]);
|
||||||
|
|
||||||
// Combine custom style with borderRadius if provided
|
// Combine custom style with borderRadius if provided
|
||||||
const combinedStyle = {
|
const combinedStyle = {
|
||||||
...style,
|
...style,
|
||||||
@@ -142,7 +149,6 @@ const ImageWithFallback = ({
|
|||||||
transition: "opacity 0.3s ease-in-out",
|
transition: "opacity 0.3s ease-in-out",
|
||||||
objectFit: "cover",
|
objectFit: "cover",
|
||||||
...combinedStyle,
|
...combinedStyle,
|
||||||
|
|
||||||
}}
|
}}
|
||||||
alt={alt || ""}
|
alt={alt || ""}
|
||||||
width={width}
|
width={width}
|
||||||
|
|||||||
Reference in New Issue
Block a user