scrimba
Frontend Career Path
React basics
Notes & Tenzies
Tenzies: Change dice to objects
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

Tenzies: Change dice to objects
AboutCommentsNotes
Tenzies: Change dice to objects
Expand for more info
App.js
run
preview
console
import React from "react"
import Die from "./Die"


export default function App() {
/**
* Challenge: Update the array of numbers in state to be
* an array of objects instead. Each object should look like:
* { value: <random number>, isHeld: false }
*
* Making this change will break parts of our code, so make
* sure to update things so we're back to a working state
*/

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

function allNewDice() {
const newDice = []
for (let i = 0; i < 10; i++) {
newDice.push(Math.ceil(Math.random() * 6))
}
return newDice
}

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

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

return (
<main>
<div className="dice-container">
{diceElements}
</div>
<button className="roll-dice" onClick={rollDice}>Roll</button>
</main>
)
}
Console
!
"Warning: Each child in a list should have a unique "key" prop. Check the render method of `App`. See https://fb.me/react-warning-keys for more information. in Die (created by App) in App"
,
/index.html
-4:32