Last active
June 7, 2021 10:06
-
-
Save qkreltms/6a2a29aa182f543678a552b0ae7c2a96 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function CandyDispenser() { | |
const initialCandies = ['snickers', 'skittles', 'twix', 'milky way'] | |
const [candies, setCandies] = React.useState(initialCandies) | |
const dispense = React.useCallback(candy => { | |
setCandies(allCandies => allCandies.filter(c => c !== candy)) | |
}, []) | |
return ( | |
<div> | |
<h1>Candy Dispenser</h1> | |
<div> | |
<div>Available Candy</div> | |
{candies.length === 0 ? ( | |
<button onClick={() => setCandies(initialCandies)}>refill</button> | |
) : ( | |
<ul> | |
{candies.map(candy => ( | |
<li key={candy}> | |
<button onClick={() => dispense(candy)}>grab</button> {candy} | |
</li> | |
))} | |
</ul> | |
)} | |
</div> | |
</div> | |
) | |
} | |
export default CandyDispenser |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment