I'm trying to make a clone of netflix, when I have the mouse over a image of the movies the trailer of that movie appears, there appears the problem when i mouseover i have many same outputs in the console. I think this problem is because im rendering inside a map, but im looking for avoid that for the performance... I tried with React.memo but doesn't work.
Here is an example
I have a parent component
function MovieList(props) {
...
<Movies movies={netflixMovies} title="Movie Feature" />
}
Inside the child component i have the following
function Movie(props) {
...
const showMiniVideo = (id) => {
setTimeout(() => {
fetchYoutubeVideo(id) // Function who give me the ID of youtube video
.then(res => {
if (res.results.length === 0) { // If doesn't exist a video i set existVideo false
setExistVideo(false)
return setIdYoutubeVideo() // IdYoutubeVideo none
}
else {
setExistVideo(true) // If exist a video i set existVideo true
res.results.map(thriler => {
return setIdYoutubeVideo(thriler.key) // IdYoutubeVideo id
})
}
})
.catch(err => {
setExistVideo(false)
console.error(err)
return setIdYoutubeVideo()
});
}, 1000);
}
...
return (
{movies.map(movie => (
<div key={movie.id} className="tile">
<div onMouseOver={() => showMiniVideo(movie.id)} onMouseOut={stopMiniVideo}>
{idYoutubeVideo ?
(idMovie === movie.id ?
(existVideo ?
console.log("hay video")
:
console.log("no hay video")
)
:
console.log("no es el id")
)
:
<svg style={{ color: 'white' }} width="1.5vw" height="1.5vw" viewBox="0 0 16 16" className="bi bi-play-fill" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
<path d="M11.596 8.697l-6.363 3.692c-.54.313-1.233-.066-1.233-.697V4.308c0-.63.692-1.01 1.233-.696l6.363 3.692a.802.802 0 0 1 0 1.393z" />
</svg>
}
</div>
</div>
</div>
))}
)}
Any one have an idea? I would appreciate any help

I think the issue you have is not understanding the javascript listener
mouseoveritself. Mouseover get's called (pretty much) every time you move the mouse over the element you are listening on. What you probably want ismouseenterandmouseleavewhich both exist in react asonMouseEnterandonMouseLeave. The main difference here is that they both only fire once, once the pointer enters the area of the element, or respectively leaves it.By the way, the MDN actually explains this behavior in the article about mouseover.
Feel free to get back to me in the comments, and happy coding =)