From f456fd7ebd8bf8ee904daacfb16080f2eddd9ba0 Mon Sep 17 00:00:00 2001 From: "Kent C. Dodds" Date: Fri, 23 Feb 2024 09:14:15 -0700 Subject: [PATCH] sync external --- exercises/08.focus/01.solution/index.tsx | 30 +++++++++---------- .../09.sync-external/01.problem/README.mdx | 1 + .../09.sync-external/01.problem/index.css | 24 +++++++++++++++ .../09.sync-external/01.problem/index.tsx | 14 +++++++++ .../09.sync-external/01.solution/README.mdx | 1 + .../09.sync-external/01.solution/index.css | 24 +++++++++++++++ .../09.sync-external/01.solution/index.tsx | 28 +++++++++++++++++ .../09.sync-external/02.problem/README.mdx | 1 + .../09.sync-external/02.problem/index.css | 24 +++++++++++++++ .../09.sync-external/02.problem/index.tsx | 28 +++++++++++++++++ .../09.sync-external/02.solution/README.mdx | 1 + .../09.sync-external/02.solution/index.css | 24 +++++++++++++++ .../09.sync-external/02.solution/index.tsx | 21 +++++++++++++ .../02.solution/narrow-media-query.ts | 15 ++++++++++ exercises/09.sync-external/README.mdx | 1 + 15 files changed, 221 insertions(+), 16 deletions(-) create mode 100644 exercises/09.sync-external/01.problem/README.mdx create mode 100644 exercises/09.sync-external/01.problem/index.css create mode 100644 exercises/09.sync-external/01.problem/index.tsx create mode 100644 exercises/09.sync-external/01.solution/README.mdx create mode 100644 exercises/09.sync-external/01.solution/index.css create mode 100644 exercises/09.sync-external/01.solution/index.tsx create mode 100644 exercises/09.sync-external/02.problem/README.mdx create mode 100644 exercises/09.sync-external/02.problem/index.css create mode 100644 exercises/09.sync-external/02.problem/index.tsx create mode 100644 exercises/09.sync-external/02.solution/README.mdx create mode 100644 exercises/09.sync-external/02.solution/index.css create mode 100644 exercises/09.sync-external/02.solution/index.tsx create mode 100644 exercises/09.sync-external/02.solution/narrow-media-query.ts create mode 100644 exercises/09.sync-external/README.mdx 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 }} />
) : (