Explorer
project
App.js
Die.js
index.html
index.js
style.css
Dependencies
nanoid@3.1.28
react-confetti@6.0.1
react-dom@17.0.2
react@17.0.2
Bootcamp
Study group
Collaborate with peers in your dedicated #study-group channel.
Code reviews
Submit projects for review using the /review
command in your #code-reviews channel
import React from "react"
import Die from "./Die"
import {nanoid} from "nanoid"
export default function App() {
const [dice, setDice] = React.useState(allNewDice())
const [tenzies, setTenzies] = React.useState(false)
React.useEffect(() => {
const allHeld = dice.every(die => die.isHeld)
const firstValue = dice[0].value
const allSameValue = dice.every(die => die.value === firstValue)
if (allHeld && allSameValue) {
setTenzies(true)
console.log("You won!")
}
}, [dice])
/**
* Challenge: Tie off loose ends!
* 1. If tenzies is true, Change the button text to "New Game"
* 2. If tenzies is true, use the "react-confetti" package to
* render the <Confetti /> component 🎉
*
* Hint: don't worry about the `height` and `width` props
* it mentions in the documentation.
*/
function generateNewDie() {
return {
value: Math.ceil(Math.random() * 6),
isHeld: false,
id: nanoid()
}
}
function allNewDice() {
const newDice = []
for (let i = 0; i < 10; i++) {
newDice.push(generateNewDie())
}
return newDice
}
function rollDice() {
setDice(oldDice => oldDice.map(die => {
return die.isHeld ?
die :
generateNewDie()
}))
}
function holdDice(id) {
setDice(oldDice => oldDice.map(die => {
return die.id === id ?
{...die, isHeld: !die.isHeld} :
die
}))
}
const diceElements = dice.map(die => (
<Die
key={die.id}
value={die.value}
isHeld={die.isHeld}
holdDice={() => holdDice(die.id)}
/>
))
return (
<main>
{/* Render Confetti component if `tenzies` is true*/}
<h1 className="title">Tenzies</h1>
<p className="instructions">Roll until all dice are the same. Click each die to freeze it at its current value between rolls.</p>
<div className="dice-container">
{diceElements}
</div>
<button className="roll-dice" onClick={rollDice}>Roll</button>
</main>
)
}