scrimba
Frontend Career Path
React basics
Notes & Tenzies
Tenzies: Hold dice part 2
Go Pro!Bootcamp

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

AboutCommentsNotes
Tenzies: Hold dice part 2
Expand for more info
App.js
run
preview
console
import React from "react"
import Die from "./Die"
import {nanoid} from "nanoid"

export default function App() {

const [dice, setDice] = React.useState(allNewDice())

function allNewDice() {
const newDice = []
for (let i = 0; i < 10; i++) {
newDice.push({
value: Math.ceil(Math.random() * 6),
isHeld: false,
id: nanoid()
})
}
return newDice
}

function rollDice() {
setDice(allNewDice())
}

/**
* Challenge: Update the `holdDice` function to flip
* the `isHeld` property on the object in the array
* that was clicked, based on the `id` prop passed
* into the function.
*
* Hint: as usual, there's > 1 way to accomplish this.
* I'll be using `dice.map()` and checking for the `id`
* of the die to determine which one to flip `isHeld` on,
* but you can do whichever way makes the most sense to you.
*/
function holdDice(id) {
console.log(id)
}

const diceElements = dice.map(die => (
<Die
key={die.id}
value={die.value}
isHeld={die.isHeld}
holdDice={() => holdDice(die.id)}
/>
))

return (
<main>
<div className="dice-container">
{diceElements}
</div>
<button className="roll-dice" onClick={rollDice}>Roll</button>
</main>
)
}
Console
"HgEOEc0c-x3EDmq2u-mnE"
,
"ayzSnj6YTLpO7DjCujzuf"
,
"YjblmKbNmJXfHYfF6j5Jn"
,
/index.html
-3:09