2023-02-08 21:49:36 +01:00
|
|
|
import React, { useState, useEffect } from 'react';
|
2023-03-03 14:44:34 +01:00
|
|
|
import { useLocation } from 'react-router-dom';
|
2023-02-08 21:49:36 +01:00
|
|
|
|
|
|
|
|
const ImageBanner = () => {
|
|
|
|
|
const [currentImage, setCurrentImage] = useState(1);
|
2023-03-04 22:11:45 +01:00
|
|
|
const [isLoaded, setIsLoaded] = useState(false);
|
2023-03-03 14:44:34 +01:00
|
|
|
const location = useLocation();
|
2023-02-08 21:49:36 +01:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2023-03-04 22:11:45 +01:00
|
|
|
setIsLoaded(false);
|
2023-02-27 17:44:02 +01:00
|
|
|
setCurrentImage(Math.floor(Math.random() * 13) + 1);
|
2023-03-03 14:44:34 +01:00
|
|
|
}, [location.key]);
|
2023-02-08 21:49:36 +01:00
|
|
|
|
2023-03-04 22:11:45 +01:00
|
|
|
useEffect(() => {
|
|
|
|
|
setIsLoaded(false);
|
|
|
|
|
const img = new Image();
|
2023-03-05 14:26:40 +01:00
|
|
|
img.src = `/assets/banners/banner-${currentImage}.jpg`;
|
2023-03-04 22:11:45 +01:00
|
|
|
img.onload = () => {
|
|
|
|
|
setIsLoaded(true);
|
|
|
|
|
};
|
|
|
|
|
}, [currentImage]);
|
|
|
|
|
|
2023-02-08 21:49:36 +01:00
|
|
|
return (
|
2023-03-04 22:11:45 +01:00
|
|
|
<>
|
2023-03-05 14:26:40 +01:00
|
|
|
{isLoaded && <img id="banner-img" src={`/assets/banners/banner-${currentImage}.jpg`} alt="banner" />}
|
2023-03-04 22:11:45 +01:00
|
|
|
</>
|
2023-02-08 21:49:36 +01:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2023-03-05 14:26:40 +01:00
|
|
|
export default ImageBanner;
|