|
1 | 1 | # Focus Management
|
| 2 | + |
| 3 | +Helping the user's focus stay on the right place is a key part of the user |
| 4 | +experience. This is especially important for users who rely on screen readers or |
| 5 | +keyboard navigation. But even able users can benefit from a well-thought focus |
| 6 | +management experience. |
| 7 | + |
| 8 | +Sometimes, the element you want to focus on only becomes available after a state |
| 9 | +update. For example: |
| 10 | + |
| 11 | +```tsx |
| 12 | +function MyComponent() { |
| 13 | + const inputRef = useRef<HTMLInputElement>(null) |
| 14 | + const [show, setShow] = useState(false) |
| 15 | + |
| 16 | + return ( |
| 17 | + <div> |
| 18 | + <button onClick={() => setShow(true)}>Show</button> |
| 19 | + {show && <input ref={inputRef} />} |
| 20 | + </div> |
| 21 | + ) |
| 22 | +} |
| 23 | +``` |
| 24 | + |
| 25 | +Presumably after the user clicks "show" they will want to type something in the |
| 26 | +input there. Good focus management would focus the input after it becomes |
| 27 | +visible. |
| 28 | + |
| 29 | +It's important for you to know that in React state updates happen in batches. |
| 30 | +So state updates do not necessarily take place at the same time you |
| 31 | +call the state updater function. |
| 32 | + |
| 33 | +As a result of React state update batching, if you try to focus an element right |
| 34 | +after a state update, it might not work as expected. This is because the element |
| 35 | +you want to focus on might not be available yet. |
| 36 | + |
| 37 | +```tsx remove=10 |
| 38 | +function MyComponent() { |
| 39 | + const inputRef = useRef<HTMLInputElement>(null) |
| 40 | + const [show, setShow] = useState(false) |
| 41 | + |
| 42 | + return ( |
| 43 | + <div> |
| 44 | + <button |
| 45 | + onClick={() => { |
| 46 | + setShow(true) |
| 47 | + inputRef.current?.focus() // This probably won't work |
| 48 | + }} |
| 49 | + > |
| 50 | + Show |
| 51 | + </button> |
| 52 | + {show && <input ref={inputRef} />} |
| 53 | + </div> |
| 54 | + ) |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +The solution to this problem is to force React to run the state and DOM updates |
| 59 | +synchronously so that the element you want to focus on is available when you try |
| 60 | +to focus it. |
| 61 | + |
| 62 | +You do this by using the `flushSync` function from the `react-dom` package. |
| 63 | + |
| 64 | +```tsx |
| 65 | +import { flushSync } from 'react-dom' |
| 66 | + |
| 67 | +function MyComponent() { |
| 68 | + const inputRef = useRef<HTMLInputElement>(null) |
| 69 | + const [show, setShow] = useState(false) |
| 70 | + |
| 71 | + return ( |
| 72 | + <div> |
| 73 | + <button |
| 74 | + onClick={() => { |
| 75 | + flushSync(() => { |
| 76 | + setShow(true) |
| 77 | + }) |
| 78 | + inputRef.current?.focus() |
| 79 | + }} |
| 80 | + > |
| 81 | + Show |
| 82 | + </button> |
| 83 | + {show && <input ref={inputRef} />} |
| 84 | + </div> |
| 85 | + ) |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +What `flushSync` does is that it forces React to run the state update and DOM |
| 90 | +update synchronously. This way, the input element will be available when you try |
| 91 | +to focus it on the line following the `flushSync` call. |
| 92 | + |
| 93 | +In general you want to avoid this de-optimization, but in some cases (like focus |
| 94 | +management), it's the perfect solution. |
| 95 | + |
| 96 | +Learn more in [the `flushSync` docs](https://react.dev/reference/react-dom/flushSync). |
0 commit comments