scrimba
Frontend Career Path
React basics
Notes & Tenzies
Tenzies: Styling held dice
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: Styling held dice
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() {
/**
* Challenge: Add conditional styling to the Die component
* so that if it's held (isHeld === true), its background color
* changes to a light green (#59E391)
*
* Remember: currently the Die component has no way of knowing
* if it's "held" or not.
*/

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())
}

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

return (
<main>
<div className="dice-container">
{diceElements}
</div>
<button className="roll-dice" onClick={rollDice}>Roll</button>
</main>
)
}
Console
/index.html
-3:38