| 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 developer, I want to organize my code into reusable components so that my application is easier to maintain and understand.
Extract the stats display into its own component that accepts props for score, lives, and styling.
Stats component above the App componentscore, lives, and style as propsApp, replace the stats div with <Stats score={score} lives={lives} style={statsStyle} />Need help with components and props? Check out these snippets:
function Stats({ score, lives, style }) {
return (
<div className="stats" style={style}>
Score: {score} Lives: {lives}
</div>
)
}
function App() {
// ... all your state and functions ...
const statsStyle = {
backgroundColor: "brown",
color: "whitesmoke"
}
// ... conditional styling logic ...
return (
<>
<Stats score={score} lives={lives} style={statsStyle} />
<div className='orchard-background' onClick={missTarget}>
{ score < 100 ?
<div className="apple-target" onClick={clickTarget} style={appleStyle}></div>
: <h1 className="win"> You Win!</h1>
}
</div>
</>
)
}
import { useState } from 'react'
import './App.css'
function randomNumber(a, b) {
return Math.floor(Math.random() * (b - a) + a);
}
// NEW: Stats component
function Stats({ score, lives, style }) {
return (
<div className="stats" style={style}>
Score: {score} Lives: {lives}
</div>
)
}
function App() {
// ... rest of App component
}
export default App
Understanding Props:
{ score, lives } to extract propsWhy Extract Components?