1const FocusDemo = () => {
2
3 const [inputRef, setInputFocus] = useFocus()
4
5 return (
6 <>
7 <button onClick={setInputFocus} >
8 FOCUS
9 </button>
10 <input ref={inputRef} />
11 </>
12 )
13
14}
15
16const useFocus = () => {
17 const htmlElRef = useRef(null)
18 const setFocus = () => {htmlElRef.current && htmlElRef.current.focus()}
19
20 return [ htmlElRef, setFocus ]
21}
22