Let’s wire up the MessagePopup
so it shows when the game is paused or over, and hides otherwise.
- Access
isRunning
andgameOver
from Redux - Dynamically show or hide the message popup
- Display appropriate message text
Open src/components/MessagePopup.js
and import:
import { useSelector } from 'react-redux';
Then, get the state values inside the component:
const { isRunning, gameOver } = useSelector((state) => state);
✅ You now have access to the game’s status.
Use logic to show the popup and change the message:
let message = '';
let isHidden = 'hidden';
if (gameOver) {
message = 'Game Over';
isHidden = '';
} else if (!isRunning) {
message = 'Paused';
isHidden = '';
}
✅ When the game is paused or over, the popup appears. Otherwise, it’s hidden.
import React from 'react';
import { useSelector } from 'react-redux';
export default function MessagePopup() {
const { isRunning, gameOver } = useSelector((state) => state);
let message = '';
let isHidden = 'hidden';
if (gameOver) {
message = 'Game Over';
isHidden = '';
} else if (!isRunning) {
message = 'Paused';
isHidden = '';
}
return (
<div className={`message-popup ${isHidden}`}>
<h1>{message}</h1>
</div>
);
}
✅ You’re now conditionally showing messages based on game state using Redux.
- Temporarily set
isRunning: false
orgameOver: true
indefaultState()
to test different messages. - Change the CSS for
.message-popup
or.hidden
to visualize behavior more clearly.
"Why is
useSelector
more flexible than passing props down toMessagePopup
?"
"What are other ways to conditionally render components in React?"
"How would you animate the appearance of this popup with CSS or a library like Framer Motion?"
- What’s the role of the
hidden
class? - What happens if both
isRunning
andgameOver
are false? - Why don’t we need to pass anything to
MessagePopup
via props?
git add .
git commit -m "Added connection for message popup"
git push