Skip to content

Instantly share code, notes, and snippets.

@qkreltms
Last active June 7, 2021 10:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qkreltms/6a2a29aa182f543678a552b0ae7c2a96 to your computer and use it in GitHub Desktop.
Save qkreltms/6a2a29aa182f543678a552b0ae7c2a96 to your computer and use it in GitHub Desktop.
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