I would like to pass a style or a class to an element of an array when this element is on hover. My issue is: whenever I hover one element of this array, the class or the style is added to each element of one array whereas I want only to apply the style or class to the element on hover. Here I want the tag P to appear whenever his parent DIV is on hover.
Note: I did try to play with the id but I cannot find a way to add a class within the function. It seems in react that it is more common to handle this change within the render method. So, how can I do it without the spread of this style or class to each element of the array? Thank you very much.
this.state = {
meals: [
{
src: meal1,
id: "meal1",
alt: "Burger",
name: "Name1",
description: [
"Thick slices of French",
"bread stuffed with cream cheese",
" fried in egg",
"Accompanied by Maine maple syrup",
"sausage links"
]
},
{
src: meal2,
id: "meal2",
alt: "Sandtwitch",
name: "Name2",
description: [
"Cheddar and Monterey Jack cheesesslices of French",
"on grilled toast",
" fried in egg",
"Accompanied by Maine maple syrup",
"sausage links",
"warm tomato soup or Gazpacho"
]
},
{
src: meal3,
id: "meal3",
alt: "Sandtwitch",
name: "Name3",
description: [
"Cheddar and Monterey Jack cheesesslices of French",
"on grilled toast",
" fried in egg",
"Accompanied by Maine maple syrup",
"sausage links",
"warm tomato soup or Gazpacho"
]
}
],
onHover: false
};
}
paragraphAppear = () => {
this.setState({
onHover: !this.state.onHover
});
};
render() {
return (
<section className="gallery_section">
<h1> Some pics of our plats</h1>
<div className={"meal_container"}>
{this.state.meals.map((element, index) => (
<figure key={element.id} onMouseOver={this.paragraphAppear}>
<img src={element.src} onClick={this.effectPictures} />
<div
id={element.id}
style={
this.state.onHover
? { display: "block" }
: { display: "none" }
}
>
<p>{element.name}</p>
</div>
</figure>
))}
</div>
</section>
);
}
}