- Podcasts
- The React Show
- React 18: FINALLY concurrent execution!
React 18: FINALLY concurrent execution!
The React Show
- July 1, 2021
- 40 min
- Season 1, episode 5
About this episodeIn this episode we talk about the upcoming React 18 release. This is a very exciting release, especially from a performance perspective…
In this episode we talk about the upcoming React 18 release. This is a very exciting release, especially from a performance perspective, as it finally brings concurrent and interruptible rendering along with other exciting changes!
Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
React 18: FINALLY concurrent execution!
And welcome to the React Show. It's gonna be a really fun one today. Uh React is coming out with a new release. Uh we don't know exactly when yet, but they've uh announced the details. It's a slightly old news a few weeks ago, but we're we're still gonna talk about it. Um yeah, so that's exciting. Um we we are uh well I am Thomas and and I'm with my co-host Austin here with Owl Creek, uh high performance react consultancy. Yeah. Um and I see on my show notes here I'm supposed to talk about this high performance React book that uh we recently published. Yeah, so if you're so if you're uh interested in that, uh yeah, please check out our website, owlcreek.studio. It's linked to there. Uh and and you can learn in depth about uh how React works and and how to get good performance out of it. Um we also uh set up an email address so you could uh send us feedback about the show. So the React show at owlcreek.studio would be the place to do that. You can send us questions or feedback. Um and we're also excited that today will be the first show. We we do a little QA at the end from one of our listeners. We had a question to go over. Um so yeah, if you'd like to also uh have us discuss your your question, please send it to the React Show at Owlcreek.studio. And yeah, so here we go. Uh welcome, Austin. How are you doing today?
Yeah, the it there is an alpha release. The the final release they haven't announced when that will be. Um and what's really, I think, big news uh is that you can update incrementally to the new features in this version of React. Uh previously, when they had announced concurrent mode, which this is this release is related to, it was more of an all or nothing type of thing. Um, they've sort of broken it down and made it a lot easier to incrementally adopt uh some of these new features, which I am excited about. I think that's always a almost always a preferable approach. And I know from reading what they wrote that a lot of people are also excited to be going that route instead. So that's cool.
Well, that's one of the things I like about React in general, is they kind of did that with the entire hooks paradigm as well. Like you didn't have to use them, but you could bring them on board if you like. Um, it makes this library become even more and more attractive to use in our in our field, which I'm a big fan, and I'm glad that this is also taking that same route.
Yeah, um it's it's always a trade-off because uh it can be slower to develop new features that way. Um and so if you're trying to keep up keep up with the Joneses, you know, the other you know, competitors of React or whatever they might be, that can be challenging. Um But I think you know, there's obviously things missing from React, but I think in general they've struck up a good balance. And it definitely makes it way, way easier as a business to adopt something when you know you can just keep using it for a long time and they're not gonna just drop whatever feature you're on, you know, next year. Um huge, huge benefit. Uh so yeah, I think we can get into the main uh the main point of React 18, which is concurrent rendering. Uh now, if you're not that familiar with how React works internally, that might not mean much of anything to you. Um so I will I will describe you know what that means and why it's so exciting. Um I think the the first way to do that would be actually to look at the example that they gave in um I don't know, one of their things here that I that I have on it. Uh basically, so like let's say you have a uh like an autocomplete for you know countries of the world or products or something like that. You have an autocomplete form that you have to type into. Um when you type keys on the keyboard as a user, you expect those keys or those keystrokes to show up in that box right away, right? Like you don't want to it it's a bad experience if you type the letter A and then wait a second, you know, before A shows up. But what if your drop down that shows like products or countries or whatever it depends on this long-running background query or whatever. Um and then inside the the actual box, you know, is a really complicated React set of React components and it's kind of slow to render. Now, if you're on like a weaker device, like say an older mobile phone, typing each letter is gonna be delayed by default. Like you type A and you're gonna sit there and wait for like a couple seconds while it re-renders the whole thing. Um, and then the A will show up, and then the whole everything shows up. You type it up, it's a horrible user experience, right? Um the way that we've sort of tried to get around that in the React community is basically using set timeouts. So we'll try to like get the letter A to show up in the box and then do a set timeout for like all this other work that needs to be done. Um the problem is if it takes a second on a mobile phone to render all that content, you're gonna have a second of a delay sometime, right? Like there's no way around it. Maybe you can get that letter up there first so it looks like it's working better, but if the user types really fast, it's just it's gonna be a mess because you just have to wait. It takes a second to render. The way React works is it starts at um either the top of the tree or where state changed in the render tree and just renders down from there. And once it starts rendering, it doesn't stop rendering until it's done doing its rendering. And that's why you end up in situations like this where you have performance bottlenecks on things that maybe don't need to happen immediately. But React has no way previously to suspend that rendering and do more important things. Like that's basically the the entire point of React 18 is you can suspend rendering now and do more important renderings in between. So I could be like halfway done rendering that box of components or whatever, and then oh, the user typed another character. Suspend rendering of that box because that's not as important. We're gonna draw another character in the text box, and then go back and resume that rendering we were doing earlier. So now doing things like that where you have a performance bottleneck that is not really something that needs to happen immediately, can be handled much, much better in the new version of React. How does that sound to you, Austin?
Yeah, absolutely. I think it it does require using a uh uh this new API, but it looks really pretty simple and easy to use. So you will have to enable React 18 and then use this new API. But once you do, a lot of these issues will just magically get fixed. And that's what's so exciting to me is they aren't like requiring you to learn um threading or something to take advantage of these features. A lot of them, it just works. React will will handle it for you. Um so yeah, we can we can talk about like the individual um features. So the first one is uh start transition. So start transition is basically what we were talking about now. Um let's say you've got that input box and there's a part of it that doesn't need to happen immediately. You will put that inside a start transition uh call. And then once you do that, React knows that everything that happens inside of there is not urgent. It can suspend it in the background and just handle it when it's ready. Um, so it's a pretty simple API and it will help the situation where you have slow renders a lot. Now, like anything performance related, you still have, in a sense, a performance bottleneck because stuff still has to get computed at some point. So, you know, ideally you would eliminate the bottleneck so that it doesn't take a second or whatever. You know, that's probably your ideal situation, but that is often a lot more work. This this would be a simple API to get around the issue and probably be good for like 80% of cases, I would say. Um so yeah, basically it the start transition API divides React rendering into two types of rendering, urgent renders and transition renders. Urgent renders, React will complete them right away and complete them thoroughly. Transition renders, it can interrupt and suspend, and it will also allow you using a hook to find out if a transition render is occurring and display like uh a spinner or a progress bar or something like that. So it sounds like a really convenient uh API for working with this type of situation.
Yeah, I see the the is pending example that they have in the documentation. That's quite nice to be able to. I know that we we use these sort of loading indicators and like placeholder objects within the UI when we're waiting on data, but to be able to use them when we're actually dealing with rendering based on the browser, the computer speed or the network speed is definitely a pretty interesting upgrade.
Yeah, I I think it'll be awesome. I'm super excited for that. Um yeah, so let's see, what else do I have in my notes here? Um, I was just gonna say it's it's better than set timeout because the actual render itself can be interrupted and you don't have to, yeah, it's not not a hack, basically.
Yeah, I was just about to hop to that. So um, yeah, perfect hiring. Um so automatic batching is also a huge update. In fact, this one is probably a bigger update in the sense that people will adopt it probably right away, and it's easy, like you don't have to do anything, it'll just work, and the benefit will be huge. So the whole point is like uh say in React you make a state change. Um so if React did no batching at all, and you made three like let's say you have um I click a button, it hits an event handler, right? And in my event handler, I have three state changes. If React did not do batching, each of those state changes would have a render in between each of them. So state changes, React renders your component. State changes, React, and that would be kind of really sucky performance if you do a lot of state changes. And it can also be weird because maybe you'll get your component in a situation where the state doesn't really match up because you were trying to change all that state at once. Now that's kind of bad design on your part. You should, if you want state to change at once, you should change it in one call, but basically nobody does that in React unless they they use reducer or something like that, and it's not as common. Um but yeah, currently in React, if you have an event channeler and you have multiple state changes, uh React does batch those together. So it will only uh re-render your component once. Now, what if you need to make a background like API call or you have a callback, or you you do anything else that delays execution in some way or or whatever from this event handler? Now, all of the state changes and stuff that happen in that bit of code are not batched. And so this is definitely an issue that you'll see a lot with like um hooks and such where things change and you have networking calls. Uh I've definitely seen lots of code where people are like, oh, you know, we need to like combine these state transitions or we need to do these other weird things. Or there's uh actually I've seen uh really common is a there's an internal API, I forget what it's called off the top of my head, but oh yeah, here it is. Unstable batched updates. So you'll see this a lot in the code, which is basically telling React, like, yeah, combine the batch these updates. Um and it's hacky and obviously unstable and undocumented, but people use it all the time.
I it's well I'm looking at the example and it shows that like if you handle a click and within the click you fetch something and then you add a then statement to that that promise return. Um that in 17 those the the state updates within that are not. So that being said, that is like a promise, right? So uh yeah, that answers our question.
Well, that's the the bad part about the current you know version before 18 is it's kind of uh unknown whether it's batching it really or not, unless you really know these rules. Like unless you you really know them, you're not really sure, and it's kind of a mess. Now, React 18 is exciting because they just made it work everywhere for everything. I mean, that is I'm I'm impressed. I don't know, it sounds it sounds impressive to me at least. That's that's fantastic. Now we can just and I think that's the way it should be. You should be able to just use the API and have it handle things like this. And so I'm really super excited to see that that's how they implemented it. I was concerned they would you know use that unstable, undocumented API instead and like require you to like manually batch things, which I think would be a very subpar solution. This is great. I I'm super happy about automatic batching. Um yeah, so uh any anything else you had to say on that? Otherwise, we can move on to uh another huge thing that I I think I'm really excited about, but no, I I'm pretty stoked about that as well. Sweet. Yeah, so the last of the three major uh changes that they're introducing is streaming server-side rendering. And this kind of combines the other updates and then adds more, but I I think it's a really awesome piece of technology. I I gotta see how it works in practice. If it works well, this could, I mean, really revolutionize how how we write front-end code, or at least how it functions. Um so basically the idea is right now for server-side rendering, how it works is a user's like, hey, I want to view your page. So the server's like, okay, and it renders all it does a render on your React components on the server and takes the generated HTML and all the JavaScript needed to run the page and sends it back to the user's browser. Now, the user's browser will show the HTML. So the user, it's great because the user gets more immediate feedback. They can see the content, but they can't interact with it yet. They can't interact with it until it's hydrated. And hydrating is depends on the JavaScript getting loaded, and then React will go through and basically do another render except attach the uh output to the existing DOM elements instead of creating new ones. So it's more performant when you load and the user gets to see something right away, but it's also subpar because let's say you have a big you know server-side application or you have a big React application rendered on the server. You know, there are things to like split up bundles and stuff, but generally a very large portion of your program it has to be rendered on the server. And let's say you need data from like a database or another API or whatever, you have to wait for all that data to come in before you can complete rendering on the server, right? Like you can't, like the it doesn't allow you to just send back parts or whatever. So you have uh a bit of a slowdown there. In fact, it could be a massive slowdown if you have delays in some API or the database or whatever, before you can even generate the HTML to send back to the user. But now that you've done that, let's say you've generated HTML, sent it back to the user, the user has to wait until React hydrates the entire thing before they can interact with it. Even if they don't care about like the 80% that's below the fold or whatever, doesn't matter. React needs to hydrate the entire thing. So streaming server-side rendering coming in React 18 is really exciting because it basically allows us to split all of those stages into different chunks. So like on the in your code, you can basically like, you know, I I forget if it's it uses, I think, the suspense API. Um, so you can uh basically tell React, hey, this chunk of code is you know, can be delayed, you can load it later, right? So like maybe your header and your main nav bar and the first you know input or whatever you want to be done right away, but then you've got like comments or something, you know, that aren't as important. In the old way of doing it, you still had to render the comments component and get all the comments or like manually like only load comments on the client side or whatever. Like if you wanted comments to not slow down things. Um, and the new one you can just put in suspense, and then when React is rendering on the server, it will actually not depend on that part being rendered before sending HTML back to the user's browser, which is amazing. So now you can just send back the important stuff right away, and then React will actually stream the updates as they come in. So, like let's say comments takes 10 seconds to load from the database. When that 10 seconds expires and React gets that data, it will then render on the server your comments and send them to the browser, and then on the browser side, React will be like, oh cool, I got the comments component now, I'll I'll hydrate that. Um and so this basically allows you to it should allow you to massively cut down the loading time for React. Applications and make it so that the time to interactive, you know, when the user can actually interact with something will be much, much improved if people actually implement this well uh in their application, because then the user can start, they don't have to wait for near as much to get loaded.
Yeah, uh it it looks great. Like I'm I'm super happy with the API. Um I don't I'm I'm like kind of like this is a pr I to me it sounds like a pretty difficult thing to do well. So I would want to see how the actual implementation like works out. Um but I guess given uh Facebook's current history with React, uh, you know, it'll probably be good.
Yeah. Yeah, I I'm super excited because like I I mean I know I I'm also interested too, like, um so I often use Next.js uh, you know, and work with people that do. And it sort of tries to do some of this stuff, um, but it has kind of its own APIs. So I will be interested too to see how well uh systems like that are able to adopt this new architecture because it would make a big improvement for Next to use this, but maybe it's difficult for Next to integrate it. I don't know. That that a little bit that's that's my concern with my next projects is like, am I gonna have to wait three years to to see these advantages? You know, like I don't know the answer to that, but definitely something I've been thinking about.
Yeah, it brings me back to an old topic where we talked about like making sure you you bring in libraries and you know what you're dealing with in the long term by bringing them in. And we're like building frameworks upon frameworks now, and it definitely yeah, brings into question things like this like how will how will it all work out in the future? But I mean NextJS itself, these guys have done a really amazing job at iterating forward with some cool features, and I'm sure that it'll all work out faster than three years.
Yeah, same. I'm not really saying I don't have confidence in them, it's just I wonder. I'm like, okay, it's it's not really like them either. It's more like are the new APIs gonna fit with their APIs? And if they don't fit, how does that get resolved? Um I did a quick search to see if anyone is has written about it for next, and I'm not seeing anything yet, um, but yeah, definitely uh stay tuned for that, I suppose. Um but yeah, so you know that's why like uh so when we when we were starting the show, you know, Austin and I were talking, and and I was like, I'm so excited for this release. Like it doesn't, you know, maybe it doesn't have like flashy feature, like new, I don't know, new flashy features that a lot of people might identify with right away. But in terms of like the actual usability of React, these are things that I have always, always found really lacking. So like um if I compare this to writing a acute app in C, you know, we we run, you know, anytime you have a GUI application, you run into these same problems. Like we don't want to block the UI thread, is basically how it works. In JavaScript, it's even more weird, but generally it's the same concept. You don't want to block the main thread that's putting stuff on the screen, otherwise it gets laggy and bad user experience or whatever. So historically, it's like, oh no, no big problem. We'll just like move stuff to a different thread. You know, and in JavaScript, like you can't do that with a React rendering in JavaScript. Like that's just not a thing that is doable. So we're we are dependent on React implementing this API. That's the only way it can actually be done. Um, and so yeah, I'm super excited. I I've always wished for some way of doing these things. And and now that it's here, it's really great. And I think uh like on underlying all this, the ability to do concurrent rendering is probably gonna lead to other improvements in the future, probably big improvements. Um, it's a big update. I don't I don't know if anybody will make as big of a deal about it, but I I think it's actually bigger than probably most people realize.
Yeah, it makes me how much do we take for granted? We've gotten so spoiled with with it already that we don't even realize all these amazing things. Yeah, I well apparently adoption of uh all of these features looks pretty out-of-the-box simple. Like right now, you use React DOM to render your app to a to an element. Uh, and the only thing that seems to really be changed on the front end if you want to adopt these new features is you use the React DOM create root and then use the root to render your app, and it's pretty much good to go.
Yeah, I like how they did that. Um, because then you can, like they say, you know, compare, like and make sure there's no bugs that come out because of it, or compare the performance or whatever. It's it's nice that you can switch between the APIs like that. I do find it a little weird the way they like the way you do it. It seems kind of implicit. Like, I would think like, you know, having like a parameter you pass into React DOM or something to set the version would make more sense to me. This seems kind of like a weird, like we want to make this API change, and we're just gonna piggyback on that to imply you want React 18. You know what I mean? Like, seems kind of weird, but I guess whatever it works.
Well, it's not so much uh impressed the my my thought of them not sleeping. It's more of just like I wake up and there's always something new to learn because there's always some new thing that someone over there has made, and it's like, okay, yeah, if I don't if I don't work on this, I'll be irrelevant in my job pretty soon. So gotta keep learning, which is it's a good thing.
That's true, actually. In that sense, I I I mean, a factor probably of them hiring many programmers and actually funding this type of thing, having the money to do it. Um, I would love to to fund development of things like this. I I need a few more donations to to Owl Creek or something, I don't know, before we could do that. But yeah, we're on our way there. Yeah. Yeah, very true. Um yeah, anyway, so I think um that's most of what I had uh set up here for talking about for concurrent rendering in React 18. They do have other um things that are related, like use deferred value, suspense list. Um so if those sound interesting to you, you can look them up. Um it's also uh, you know, like they say here, it's all these changes are powered by cooperative multitasking, priority-based rendering, scheduling, and interruptions, which when I read that, my thought immediately went to like operating systems, you know, like schedulers for like CPUs and things like that, because that's exactly what they do. Um, you know, they make it so that we can run three programs at the same time on one CPU core, you know, by using techniques like that. Um, so to me it was like really interesting just to see, oh yeah, it's kind of taking on the characteristics of uh CPU or a scheduler, I guess. Uh kernel scheduler would be more more accurate. But yeah, I thought that was interesting. Um what we're getting.
RB thinks that these people that are like developing these libraries, a lot of them started out perhaps with JavaScript initially, and they then they began to like they're kind of backwards or reverse engineering how how programming and hardware and software in general work, and like rediscovering these paradigms from from things that already exist and taking them into what is now being built to improve what what is now being built.
Yeah. Yeah, and I think too, it's like natural convergence as well. Like it you run into the same problems, and so naturally the same solutions that we've done before that have worked well will be used again. Um, it's just it's just always interesting to me to see that convergence, though, I guess. That that's mostly all I'm saying about this. I find it interesting.
Um, but yeah, so I think that's what we have uh for the main show today. Uh and we did have uh a question uh from a listener, Marissa in Budapest. Um so they were asking us back uh so in our second episode when we're talking about debugging React applications, they're saying, can't rubber ducking be good sometimes? And if you're not familiar with it, rubber ducking is basically means like reading through every line of code, uh ostensibly telling a rubber ducky, you know, what the line means, you know, or someone else basically. Um, and so I I don't remember exactly, you know, how we were saying things in that episode, but I would actually agree. Uh rubber ducking is certainly something that I do. Uh okay, I'm lying. I don't do it that much anymore. I probably, I think I probably should. I used to do it a lot. I used to do it a ton. Um, like when I talked about printing off code, I would print off code and and go through every single line and make sure I understood what it meant. And I think in that episode when we talked about debugging React apps, it was like, you know, talking about how to do it like efficiently, like divide and conquer and stuff. Um and I think that's a good point. Um, that's not maybe always your goal when you're debugging. Maybe your goal is to weed your way through it and figure everything out. Um, maybe maybe your goal isn't to just solve it as quickly as possible and move on. Um, and so I think, you know, in general, rubber ducking is a great idea. And I think more people should do it, and I think I should do it more. Um, I don't know what you think about that, Austin.
Well, I think uh I think it got brought up because I got in a bad, I was joking about a bad habit I have of uh I get stuck on something and then I use someone else's a rubber duck and I start typing out into the chat with them what's going on, and within like five to ten seconds of writing out what is going on, I am like, oh shoot, I should just delete everything I wrote because I just figured it out through through the rubber ducking. So I I think the main thing is uh I taught for a a code school called Newcamp at one time, and they during their education process they said try to like there's like a three-step process for rubber ducking, is like first try to rubber duck it yourself, uh then use Google to see or any other search engine, preferably Google.
I need to I need to start using it, I guess. Um maybe they'll be our first podcast sponsor, right? Um and then after you after you've already like at least put in some effort on your your end to try to understand, then you can bother someone. So that's the only thing about rubber ducking with a human being that uh I have to say is like don't just don't just assume that that person is there for you to just like fire fire questions at without having put in a little bit of rubber ducking on your own.
I don't know about that. I I actually I really like that because it forces me to get better at jumping into other code and trying to actually understand it. Um so that's why like I purposefully don't discourage people from doing that, even if it's like you know, so-called interrupting me from whatever I was working on. What I found is that type of thing is really like forcing my brain to actually understand the code versus like if I'm just like coding, I might be lazy and I'm like, eh, I'll just like add my code here, and I'm not gonna like read through everything and try to figure it all out, you know. But when somebody like asks you like a debugging question like that, you're like, I I gotta figure, I gotta do it. I can there's no way around this. I have to actually kind of understand what's going on. And I and you feel that pressure to like kind of do it quickly, so you have a response. So you're like, it really gets your brain in that, like, all right, jump in and get an overview of it and look for like, you know, try to find the area that the relevant area, that kind of thing. Like I found it's a good brain exercise. Um, but and the other part of this, I find it helps me learn the code base a lot better because it's often areas of the code that I'm not working in, or or even like a code base I'm not involved in, but I learn as well. So like you're talking about like, oh yeah, you know, I I learned so much when I ask you questions or whatever. I learn a lot when you bring me this code because I'll be like, oh, I actually didn't know that's how this worked, or I didn't know what was going on here. You know, like I learn a lot as well, so I like it.
Sounds good. Um, yeah. So if you if you want to, you know, have my opinion on those kinds of things, you know, and work with us, you know, hit us up, you know, uh at owlcreek.studio, you know, we'd love to work with you. And if we're working with you, you know, feel free to send me debugging questions. Happy to take a look.
Yeah, I mean it's something we've you know had available and have done, but uh we I just added it to the website. Um basically if you if you uh have a performance bottleneck, um you know, we we'd be happy to take a look at it. We we have a three-step process. The first two are just a flat rate to diagnose um and then find solutions. Um yeah, so if you want to know more about that, uh again at Owlcreek.studio. Um yeah, is there anything else you want to say about that?
Yeah, that's uh a great way to end it here. I I hope uh everyone's enjoyed the show. And yeah, we'd absolutely love to hear from you. If you have you know criticisms or or questions, hit us up and and let us know. Um hope you have a good rest of your your day. Uh and yeah, that's it. See you later.
