| Level Navigation: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18⚡ | 19 |
User Story: As a player, I want to see my score increase when I click the apple so that I can track my progress.
Add React state to track the score and create a click handler for the apple.
useState from Reactscore with initial value of 0clickTarget function that:
event parameterevent.stopPropagation() to prevent click bubbling (see Level 6 “Show Me” section for details on how this works)onClick handler to the apple-target divNeed help with state and events? Check out these snippets:
import { useState } from 'react'
import './App.css'
function App() {
const [score, setScore] = useState(0)
// ... rest of component
}
function clickTarget(event) {
event.stopPropagation(); // Prevents triggering parent clicks
setScore(score + 1)
}
return (
<>
<div className="stats">
Score: {score}
</div>
<div className='orchard-background'>
<div className="apple-target" onClick={clickTarget}></div>
</div>
</>
)
.stats {
background-color: brown;
color: whitesmoke;
width: 500px;
padding: 5px;
}
useState is imported correctlyonClick handler is attached to apple-targetsetScore to update stateExperiment with Box Sizing:
In your App.css, you included box-sizing: border-box on the universal selector. Let’s see what this does!
box-sizing line:
```css
Change the stats padding to something larger, like 20px
What’s happening?
Learn about box-sizing: border-box and how it affects element sizing: MDN: box-sizing
Question: Does the stats bar stay 500px wide, or does it get wider? Why?