1
- import { useCallback , useEffect , useReducer } from "react" ;
1
+ import Head from "next/head" ;
2
+ import { useCallback , useEffect , useReducer , useRef } from "react" ;
2
3
3
4
const nGuesses = 6 ;
4
5
const wordleLength = 5 ;
@@ -57,6 +58,7 @@ function reducer(state, action) {
57
58
}
58
59
59
60
export default function Home ( ) {
61
+ const inputRef = useRef ( null ) ;
60
62
const [ state , dispatch ] = useReducer ( reducer , initialState ) ;
61
63
const { guess, guessError, history, outcome } = state ;
62
64
@@ -72,6 +74,7 @@ export default function Home() {
72
74
73
75
if ( res . ok ) {
74
76
dispatch ( { type : actionTypes . ACCEPT_RESPONSE , response : jsonResponse } ) ;
77
+ inputRef . current . value = "" ;
75
78
} else if ( jsonResponse && "error" in jsonResponse ) {
76
79
dispatch ( { type : actionTypes . ACCEPT_ERROR , response : jsonResponse } ) ;
77
80
} else {
@@ -99,8 +102,22 @@ export default function Home() {
99
102
return ( ) => window . removeEventListener ( "keyup" , onKeyUp ) ;
100
103
} , [ outcome , submitWordle ] ) ;
101
104
105
+ useEffect ( ( ) => {
106
+ inputRef . current . focus ( ) ;
107
+ } , [ ] ) ;
108
+
102
109
return (
103
110
< div className = "wordle-container" >
111
+ < Head >
112
+ < title > Wordle with React</ title >
113
+ < meta
114
+ name = "viewport"
115
+ content = "width=device-width, initial-scale=1, maximum-scale=1"
116
+ />
117
+ </ Head >
118
+
119
+ < input className = "fallback-input" type = "text" ref = { inputRef } />
120
+
104
121
< Grid guess = { guess } guessError = { guessError } history = { history } />
105
122
{ outcome === "win" && (
106
123
< div style = { { color : "green" , textAlign : "center" , padding : "1rem" } } >
0 commit comments