Current section: Focus Management 36 exercises
Problem

flushSync

Loading exercise

Transcript

00:00 We've got a pretty cool component in here. I've got these two buttons just so that you can test out your focus. But this is actually also a button. So you can imagine this could be like the name of a project or the name of your cat or whatever. But it's just a button that the user could then click on.

00:18 And at that time, it actually changes into an input, which is kind of interesting. And so then you can say, Frank is the name of my cat. And then I hit Enter, and that changes it back into a button that we can focus on. So that's the idea. That's what this thing is. And as you focus on it, then, well, see,

00:37 we've got focus problems, right? When you blur it, it changes back to a button. And there are various other interactions. So you click on it. It should turn into an input. But I want it to focus right away. If you focus on it and hit Enter, it should turn into an input. But I want it to focus right away.

00:55 If you blur, it should turn back into a button. And that's fine. So there are various experiences that require some of our focus management here. And we're going to need to use Flush Sync. So we're going to use it a couple of times in this example. So as a reminder, that's coming from React DOM.

01:14 And then you call Flush Sync. You put your state updates in there. And then you can do your focus management thereafter. So let's get into it.