i'm trying to change img src on hover, so i decided to change src through state by looping through the sourecs array onMouseenter but every time i get the last element only. i read some articles and i knew that i cant't use useState inside a loop, but i can't find another way to do it. you can take a look at my code bellow
import React, { useEffect, useState } from "react";
import { Col } from "react-bootstrap";
import { Link } from "react-router-dom";
import "./productListItem.css";
const ProductListItem = ({}) => {
const imgSrc = [
"/img/products_carousel/Untitleddesign_40_460x.webp",
"/img/products_carousel/Untitleddesign_30_460x.webp",
"/img/products_carousel/SkinRejuvenation_460x.webp",
];
const [currentImg, setCurrentImage] = useState(imgSrc[0]);
const changeImageSrc = () => {
imgSrc.forEach((element) => {
setInterval(() => {
setCurrentImage(element);
}, 1000);
});
};
const handleMouseLeave = () => {
setCurrentImage(imgSrc[0]);
};
return (
<Col
sm={4}
onMouseEnter={() => changeImageSrc()}
onMouseLeave={() => handleMouseLeave()}
>
{console.log({ currentImg })}
<Link to="/" className="list-collections__item">
<div class="list-collections__item-image-wrapper image-zoom">
<img
class="list-collections__item-image"
loading="lazy"
sizes="(max-width: 740px) calc(100vw - 48px), (max-width: 999px) calc(50vw - 60px), 480px"
height="380"
width="380"
alt=""
src={currentImg}
/>
</div>
<div className="list-collections__item-label mt-2">
<div>Queen</div>
<div>LE 2,500.00</div>
{/* </>
)} */}
</div>
</Link>
</Col>
);
};
export default ProductListItem;