diff --git a/exercises/08.focus/01.solution/index.tsx b/exercises/08.focus/01.solution/index.tsx index a68cedff..eef01ff3 100644 --- a/exercises/08.focus/01.solution/index.tsx +++ b/exercises/08.focus/01.solution/index.tsx @@ -1,5 +1,4 @@ import { useRef, useState } from 'react' -import { flushSync } from 'react-dom' import * as ReactDOM from 'react-dom/client' function EditableText({ @@ -18,7 +17,7 @@ function EditableText({ const [edit, setEdit] = useState(false) const [value, setValue] = useState(initialValue) const inputRef = useRef(null) - const buttonRef = useRef(null) + // 🐨 add a button ref here return edit ? (
{ event.preventDefault() // here's where you'd send the updated value to the server - flushSync(() => { - setValue(inputRef.current?.value ?? '') - setEdit(false) - }) - buttonRef.current?.focus() + // 🐨 wrap these calls in a flushSync + setValue(inputRef.current?.value ?? '') + setEdit(false) + // 🐨 after flushSync, focus the button with the button ref }} > { if (event.key === 'Escape') { - flushSync(() => { - setEdit(false) - }) - buttonRef.current?.focus() + // 🐨 wrap this in a flushSync + setEdit(false) + // 🐨 after the flushSync, focus the button } }} onBlur={event => { + // 🐨 wrap these in a flushSync setValue(event.currentTarget.value) setEdit(false) + // 🐨 after the flushSync, focus the button }} />
) : (