File tree Expand file tree Collapse file tree 1 file changed +9
-2
lines changed
Expand file tree Collapse file tree 1 file changed +9
-2
lines changed Original file line number Diff line number Diff line change 1- import { useState } from "react"
1+ import { useState , useRef , useEffect } from "react"
22import Die from "./Die"
33import { nanoid } from "nanoid"
44import Confetti from "react-confetti"
55
66export 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 >
You can’t perform that action at this time.
0 commit comments