Skip to content

Commit 2fee363

Browse files
committed
Add useEffect
1 parent f5a3d51 commit 2fee363

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/App.jsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
1-
import { useState } from "react"
1+
import { useState, useRef, useEffect } from "react"
22
import Die from "./Die"
33
import { nanoid } from "nanoid"
44
import Confetti from "react-confetti"
55

66
export default function App() {
77
const [dice, setDice] = useState(() => generateAllNewDice())
8+
const buttonRef = useRef(null)
89

910
const gameWon = dice.every(die => die.isHeld) &&
1011
dice.every(die => die.value === dice[0].value)
12+
13+
useEffect(() => {
14+
if (gameWon) {
15+
buttonRef.current.focus()
16+
}
17+
}, [gameWon])
1118

1219
function generateAllNewDice() {
1320
return new Array(10)
@@ -59,7 +66,7 @@ export default function App() {
5966
<div className="dice-container">
6067
{diceElements}
6168
</div>
62-
<button className="roll-dice" onClick={rollDice}>
69+
<button ref={buttonRef} className="roll-dice" onClick={rollDice}>
6370
{gameWon ? "New Game" : "Roll"}
6471
</button>
6572
</main>

0 commit comments

Comments
 (0)