I currently have a child component MoodCard which is being rendered in parent component MoodPage.
I have a State Hook in the parent component const [isHover, setIsHover] = useState(false) which toggles between true or false when the MoodCard is hovered over.
In the MoodCard component, I have a conditional {isHover && ( <div className="play-button-container"> <i className="fas fa-play"></i> </div>)} which is suppose to display an icon on the MoodCard if isHover is set to true.
However I need to pass this isHover over to the MoodCard component so it works in the conditional.
The error I'm getting is: 'isHover' is not defined no-undef. I've tried passing it as props but I don't think I have the right idea on how to do so.
Sidenote: I need this to be an individual behaviour for each rendered MoodCard. So would it be better to put this logic in the mapped MoodCard component instances?
Here is the full code:
MoodCard.js
import "./MoodCard.scss";
import MoodPage from "../pages/MoodPage";
export default function MoodCard(props) {
return (
<button
className="mood-card"
onClick={props.onClick}
onMouseEnter={props.onMouseEnter}
onMouseLeave={props.onMouseLeave}
>
<figure
style={{ backgroundImage: `url('${props.backgroundImage}')` }}
></figure>
<h1>{props.name}</h1>
{isHover && (
<div className="play-button-container">
<i className="fas fa-play"></i>
</div>
)}
</button>
);
}
MoodPage.js
const MoodCardInfo = [
{
name: "Rain",
image: "https://wallpaperaccess.com/full/164284.jpg",
sound: rain,
},
{
name: "Ocean Waves",
image: "https://wallpapercave.com/wp/wp2544960.jpg",
sound: ocean,
},
{
name: "Thunder",
image: "https://wallpapercave.com/wp/wp2544960.jpg",
sound: "thunder",
},
];
export default function MoodPage() {
const [isHover, setIsHover] = useState(false);
useEffect(() => {
console.log(isHover);
});
return (
<>
<section className="mood-row">
{MoodCardInfo.map((info) => (
<>
<MoodCard
name={info.name}
backgroundImage={info.image}
onClick={() => setCurrentSound(info.sound)}
onMouseEnter={() => setIsHover(true)}
onMouseLeave={() => setIsHover(false)}
/>
</>
))}
</section>
</div>
</div>
</>
);
}