6: Async React with Andrew Clark
Andrew Clark is a developer on the React core team at Facebook who has been working on asynchronous rendering. In this episode we do a deep dive on some of the decisions behind the implementation of async mode in React 16 as well as talk about how applications can benefit from using it.
Featuring
Links
Transcript
[00:00:06] Michael Jackson: This episode of the React Podcast is brought to you by reacttraining.com. In-person, hands-on training for development teams from React community leaders and experts. Visit reacttraining.com to learn more. Hello, everybody, and welcome back to the React Podcast. I am your host, Michael Jackson. And I have a very special guest and a personal friend of mine here on the podcast today, Mr. Andrew Clark.
[00:00:36] Andrew Clark: Hello.
[00:00:37] Michael Jackson: How's it going, Andrew?
[00:00:38] Andrew Clark: Doing well. I didn't know if I was supposed to say something after hello or just say hello, but hi everyone, I'm doing very well.
[00:00:43] Michael Jackson: You know, I probably should have like said that before we started. Sorry, I'm still learning this whole thing.
[00:00:49] Andrew Clark: No, I didn't know if I should say like, hi, I'm Andrew, I do XYZ, or just just hello.
[00:00:54] Michael Jackson: Well, tell us, what do you do? Why why are you here on the podcast, Andrew?
[00:00:59] Andrew Clark: I'm here on the podcast to talk about uh the thing that I spend most of my time thinking and dreaming and fantasizing and worrying about, which is React. Um because that is what I do all day, every day, um even when I don't want to. Um no, I I I work on React at Facebook. Um I'm on the React team. Uh we are a scrappy little team of like 7 people.
[00:01:26] Michael Jackson: 7 people. Let me see if I can name them all. There's you, there's Sophie, there's Brian, there's Sebastian, there's Flarny, um, there's Dan, and there's Dom.
[00:01:41] Andrew Clark: Yeah, Dan and Dominic. Yep.
[00:01:43] Michael Jackson: That's 7 people, right?
[00:01:45] Andrew Clark: Yeah, and then we have Tom, who was our manager until Sophie took over, and he's like our, our grand manager, I suppose.
[00:01:51] Michael Jackson: Yeah, I was wondering what he's doing these days. Is he-
[00:01:54] Andrew Clark: So he he runs the React, like, I guess it's the React org or the React- yeah, I don't know. We we have such such uh complicated org charts, I always get confused. But um, he is like one level in the organizational chart up from Sophie. So he runs 3 teams. One is the React team, React Core team, I suppose, is what we call ourselves. Then there's the React Native team. And they run React Native. So that's another like group of, I think, 8 or 8 to 10 people. I can't remember the exact count. And they just do React Native stuff. So anything that's not, um, anything that's not React DOM and is not like the stuff in React that is shared across all the different renderers. So, um, which is what React Core handles. That's the team I'm on.
[00:02:39] Michael Jackson: The isomorphic stuff.
[00:02:40] Andrew Clark: Yeah. The isomorphic or universal stuff. The, that is on the React Core team.
[00:02:45] Michael Jackson: Well, what's the third team?
[00:02:47] Andrew Clark: The third team is React Data, that's like the newest team and they handle uh Relay is one of them.
[00:02:55] Michael Jackson: Okay.
[00:02:55] Andrew Clark: Uh that's I guess that's the big the big one is Relay, but they're also they also work on both the React Native teams and the React uh uh data teams work a lot on not just the frameworks but um integrating with uh product code at Facebook. Because that's really where all of these all of this stuff comes from, like React came from. So, for instance, right now the React- we're working with them, but it's primarily the React data team is working on integrating or essentially rewriting the- a really important part of Facebook, which is the comments interface that shows up below all of the posts and newsfeed. That's like one of the oldest- the existing comments interface, which we call the UFI, it stands for like Unified Feedback Interface or something like that. Um, that is like one of the oldest React kind of mini apps in existence. And it's also probably the most used cuz it's on every single, uh, post in Newsfeed, right?
[00:03:49] Michael Jackson: Oh, for sure. Yeah.
[00:03:50] Andrew Clark: And so, uh, they are, it's really old, so it has a lot of like technical, they get technical debt, I suppose. So they're rewriting that to use like modern best practices. Sorry, say that.
[00:04:00] Michael Jackson: Well, I was just gonna ask, is you, you, you went from talking about the data team. Is the data team working on that?
[00:04:06] Andrew Clark: Yeah. Yeah. That's what they're working on. Mm-hmm. Yeah. And then that's really, we're working closely with them cuz it's really important. One, just cuz if this is going to be, since it's so So important, we want to make sure they use like the best practices and everything. And then two, this is also the first surface that we are testing out uh async React, um which is that's what I am spending most of my time, really since I started at Facebook, which is about a year and a half ago, that's like what I spent most of my time working on. Um but as we especially as we're getting closer and closer to actually shipping this, um we are this is kind of the first surface in internally at Facebook that we are deploying Um, not publicly yet, but we're deploying this to like emp- um, some employees. Um, and so it's really exciting. And so we're using that as like a testing ground to figure out like what pitfalls or what things we need to be aware of as we roll this out more widely.
[00:04:53] Michael Jackson: Yeah. That's exciting. So I guess, uh, the data team would be like every, basically everybody who's working on like the GraphQL stuff, like Lee Byron and-
[00:05:02] Andrew Clark: Uh, yeah, actually I'm not, I don't think Lee is on that team cuz we have other GraphQL teams.
[00:05:08] Michael Jackson: There, there are so many people now. I just, I I I need to get to know all of these other people.
[00:05:12] Andrew Clark: Yeah, there's so there's like GraphQL on the server, right? Like there's people that manage like our server in uh infrastructure and then there's people that work on, I get, yeah, I'm not really sure. I don't actually know what exact team Lee works on. He sits next to me or near me, so, um, so somewhere in this vicinity. Yeah, there's so many projects at Facebook, it's hard to keep track of all of them.
[00:05:32] Michael Jackson: Well, so so I wanted to talk to you uh today specifically about what you said you spend most of your time on, which is Async React. A couple of weeks ago- oh, gosh. Maybe 4 or 5 weeks ago, I think you and I did a screen share where you showed me a lot of the stuff that uh that Dan actually demoed at- you didn't show me any of his demos, but you showed me kind of the the fetcher API and the— um it's called the store API or the reader API. Where it has the something.read, right? Where the thing that actually throws if it doesn't- oh, the cache API, of course, the cache.
[00:06:12] Andrew Clark: Mm-hmm.
[00:06:13] Michael Jackson: And so, you've been working on some of that. First question out of the gate, didn't anybody ever tell you that using throw for flow control is bad?
[00:06:24] Andrew Clark: Yeah, but why? Did anyone ever tell you that mixing templates and logic is bad?
[00:06:34] Michael Jackson: Ooh. Indeed, indeed. No, that was- I, I really don't have a problem with it. I was just, uh, I just wanted to kind of get your initial sort of reaction to that because I'm sure that, uh, it may- and maybe it's still sort of close to the surface. Maybe, maybe you have received like some real criticism, uh, about that decision. You won't receive any from me, so don't worry about it. We are friends here.
[00:06:54] Andrew Clark: No, it's cool. The, um, I think that's a fair like reaction. That was my initial reaction when, um, Sebastian, like, you know, we have these like really long brainstorming sessions like in our group chat or just like at lunch or just like at the end of the day. And every idea under the sun at some point eventually gets thrown out there, no matter how stupid it is. And then eventually the really stupid ones become what we actually work on. So, um-
[00:07:20] Michael Jackson: You have to be able to say, look, we have to have sort of like a safe space where we can just throw out ideas and they're not gonna get shot down immediately, right? I mean, if if if we're on the cutting edge, we have to have that kind- I think I remember Jony Ive actually talking about kind of a very similar sort of idea that they have at Apple on the design team where, you know, it you you you have to be able to let an idea breathe for for at least a few minutes. You know, you have to give it a couple of minutes before you know before you can before you can shoot it down. So so of course, you have you have this space sort of on the team. Or yeah, you you maybe uh you you talk about, I mean, I'm I'm sure maybe uh JSX, you know, back in the day, uh I think it was originally ported from uh like a PHP implementation, right? So you actually have like uh the idea of JSX in PHP, although it's been years since I've done it.
[00:08:14] Andrew Clark: Yeah, so JSX came from this thing at Facebook called XHP, uh which is just like XML syntax inside of PHP. That you use to like declaratively compose these like server-side rendered components. So yeah, basically, basically JSX.
[00:08:31] Michael Jackson: Yeah, yeah. So even that like started out as like a crazy idea at some point, right? And somebody said, hey, what if- and then it kind of grew and kind of grew on people and they're like, yeah, okay, we could do this. We could totally do this. So so so you're sitting around and somebody says, hey, or or Sebastian apparently says, hey, what if we throw a promise? And your initial reaction was what?
[00:08:51] Andrew Clark: Uh, well, I've learned to not to never like out of hand dismiss anything. Um, so my initial reaction, usually when this when we're, so it wasn't just like out of the blue one day, we're like, hey, what if we threw a promise? It's like all of the criticisms that people, uh, or all the alternative APIs that we get people suggesting on Twitter or like on GitHub when when they're thinking about Suspense, uh, I haven't seen one that we didn't already consider. Uh, so, uh, The throwing the promise thing was not like the first thing we considered. There's like a bunch of other ideas that we had, they all had various problems, and then eventually, um, you know, Sebastian just has the thought, well, what if we just throw? Like, maybe that will solve all of our problems. So then you sit there and you think, okay, I have a visceral reaction that that's bad because of all of these years of best practices and dogma that we have like built up, which are like those, you know, best practices are useful, like you want those, uh, and they're valuable to have, but um, in a moment like this, you have to learn how to separate or how to figure out and critically evaluate like why that best practice exists and maybe the trade-off of uh abandoning that and going with and uh yeah, abandoning that best practice is is worth it in the end. Because like, you know, throwing throwing in the render- uh I feel like maybe we're losing some context. So uh for people who don't know what Suspense is, hopefully if you haven't like watched Dan's um presentation, just like hit pause on this uh podcast and maybe go like go watch it.
[00:10:16] Michael Jackson: Yeah, yeah, yeah. Sorry, I was I was sort of assuming that, right? So so so let's talk about the API really quick. Really really briefly. So for people who haven't heard about it before, and and stop me if if I'm wrong with any of this, Andrew, but uh the the the way that the Suspense API works is, um you know, you create this thing called a cache, um and that's where you keep your data. And then as you are rendering, um you could go and try and read from that cache, read a value from the cache. And if the value is there, great, you get it and you can proceed with the render uh just like normal. If it's not, uh then the cache read operation actually is gonna throw, um which is no big deal because React uh is actually you know anticipating that some of these uh that some of these caches might throw. And so at that point, it's going to to catch those and know Okay, that component obviously is not yet ready to render uh until this promise resolves. At which point, once the promise resolves, we know that we can go back and we can safely render that view at that point and that it won't throw anymore. Is that basically it? Did I did I miss anything or oversimplify it?
[00:11:33] Andrew Clark: I would really recommend anyone who hasn't seen Dan's presentation to go and watch it uh and then maybe come back to this conversation. Because um I found when I as I've like explained this to other people and uh like here at Facebook even, uh that the abstract explanation, like even if the pieces, like you can follow along to each of the pieces, you really need to see the actual thing and ideally like even play with the with the actual code to really get a grasp for it. Uh I know we we haven't like published this for real yet. Um the reason I was really excited about Dan's presentation is we were finally able to take something we've really been talking about for like over a year, like Sebastian talked about all of these ideas behind Suspense. He talked about the like the motivations for it at the last React Conf. Um so really, Dan's presentation wasn't revealing anything totally new necessarily in terms of like what we were working on. It was just showing a more realized um like example of how it can look in practice.
[00:12:30] Michael Jackson: Yeah.
[00:12:30] Andrew Clark: Um so anyway, I would I would really suggest people go and watch Dan's presentation so you can try and get some idea of uh of how it actually looks once you start using this pattern in actual products. Um, but even then, I've noticed that like on Twitter that some people like still uh like they'll find the demos really cool, but I don't I don't think people are gonna totally uh it's gonna totally sink in for people until they can really play with it. That was kind of my reaction as well as when I first like used a very rough version of this implementation. Um, I was uh yeah, it was like a light bulb went off. I was like, oh yeah, I've been thinking about this in theory for a long time, but now it actually kind of clicks and makes sense. Uh-huh. But yeah, your your overview is very um very accurate, I think. The whole idea is that we want to be able to start rendering, um start start rendering a tree, uh and then you get to some component that doesn't have all of the things it needs to render yet. Like say it needs to fetch some data from the server, or it needs to wait for some uh lazy loaded uh module to resolve, like using Webpack or something, or it needs to wait for some uh you know, data to come back from a web worker, it needs to wait for an image decoder, any of these like, yeah, some, any kind of I/O, needs to wait for that to resolve. Um, previously in React, your only option was really to like render null or render a spinner and then come back to it later and then fill in, replace that spinner with something else, you know. Um, so what Suspense gives you the ability to do is rather than just render a placeholder immediately, you can tell React, hey, uh you know wait a second, don't don't uh paint any of these updates uh until uh I um until I have resolved this resource that I'm waiting for over some I/O connection. Uh and then only once everything is resolved, then you can commit the entire change all at once. Um so that's the basic idea.
[00:14:22] Michael Jackson: So the demo that uh Dan showed that specifically demos this uh Suspense thing uh that you that we're talking about now was the movie uh demo. Is that right? The one that where he's like sort of clicking into the different uh movie thumbnails and then showing sort of details about about that movie. And the cool thing about that, one of the coolest things I thought about that demo was how, you know, React is actually able to- so so he could click on multiple movies. Um, and say, you know, he clicked on one, but it was taking too long to load. So then he clicked on another one. And React would actually uh be able to cancel that first navigation and then just navigate directly to the second one that he had clicked on, right? Um, because React was aware that, you know, uh, we we were in the middle of this asynchronous operation, but then we had a request to to render a different component. And and so we're going to say, you know what, even though we already had this this first promise that we were waiting for that one to resolve, uh that's not important anymore. We're just gonna toss that out essentially, and we're going to go with the second second request. Is that an accurate way to describe it?
[00:15:42] Andrew Clark: Uh kind of. So we the way we model it is not that the first request is canceled, um but we model it as uh like they were batched together. Um so you know how if you call setState multiple times in a lifecycle method, uh there's only one re-render.
[00:15:58] Michael Jackson: Yeah.
[00:15:58] Andrew Clark: Or if you call setState multiple times in an event handler, there's only one re-render. Uh it's because we batch those upstates together. So you just end up zooming to like the final state, right? So it's the same concept here. If you get like, if you initiate some update and um it's async, so like uh you get end up getting another update before the first one has resolved, we just like batch those together. And only work on like the final state. Um, so it has the effect of it, like kind of, you can think of it as like the in the middle one was canceled, but really it's just like we, we coalesced them together.
[00:16:30] Michael Jackson: Awesome. And, and the final set state and commit doesn't happen, uh, until the second one resolves in that case, I guess. Right?
[00:16:39] Andrew Clark: Yeah, exactly. And that ended up being a really, that's a being a really like important heuristic for us, um, is that like if you're Uh, if if you're looking at this movie demo or uh say like the master-detail view on your phone and like you tap one item, uh and then like as you're waiting, you change your mind, you want to tap to another one, you don't want to transition to the to the middle one like you said. You only want to kind of- there's no point in transitioning to that intermediate state. You really just want to go to the last thing you tapped on. Uh and that's almost always what you want. There might there are some exceptions perhaps, and there's there's way to ways to implement that. Even in this model. Um, but the default behavior is you almost always just want to go to the the the last final state without rendering the the intermediate ones.
[00:17:22] Michael Jackson: Yeah, so so it seems like yeah, it seems like it would be safe to sort of assume that, yeah, uh, you know, here's some default behavior. This is the way that that you almost always want it to work. Um, I think you were, and I'm not sure if you want to discuss this or not, but there were some other kind of components that you had showed me. Uh, when we were talking about the async API, some stuff that I actually hadn't seen in um in Dan's presentation, um, that would essentially allow you to kind of customize this kind of behavior, right? Like you could set a timeout uh based on how long you were willing to wait for one of these uh promises to resolve, or you could, you know, and and and say, look, if even if even if it takes You know, say say they tap and and uh they tap on them, you know, to go with this movie example, they tap on a movie description. Uh we're not going to the movie yet because we're still waiting for it to load, but hey, the user's being patient, they're not tapping on anything else. So, you know, max we're gonna wait 500 milliseconds, and uh and if we don't load, you know, if we don't load in 500 milliseconds, well then we're gonna navigate anyway and just show them, I guess, a blank image and and they'll see some maybe some loading UI or something in that screen. Is that the um is that kind of the uh the purpose of of of those controls is basically to to sort of override the default behavior in those cases?
[00:18:47] Andrew Clark: Yeah, so like the concept is that you want to you start rendering and you hit some component that isn't ready to render yet because it's waiting on some promise to resolve. So the the idea is you want to wait for that promise to resolve before you continue before you show the actual update on the screen. Um, but you don't want to wait forever, right? It would be ridiculous to wait, you know, 10 seconds for um for most types of uh operations, right? Um, so depending on the type of thing it is, you want to give it a— uh I don't know what you want to call it, like a timeout or a threshold, after which you tell React, you know what, this is taking too long. I don't want to wait more than 2 or 3 seconds, perhaps. Uh, if it takes more than 2 or 3 seconds, just show this other view instead. Um, so that this this did show up in Dan's demo in the form of these placeholder components, and these place the placeholder components had a prop on it called delay milliseconds. Um, yeah, and so that's how that concept kind of uh manifested in Dan's demo. Um, the uh the thing that you're referring to, this timeout component, um, the naming, I I suppose, and the exact APIs were still uh like I guess Deciding on, um, but the idea is the thing that that you're referring to is just like a little bit of a lower level version of Dan's thing. Um, so, but uh yeah, there will definitely be some concept of configuring the threshold for how long to wait for things to render before you just give up and render a placeholder instead. And the way I think about this is that it's very similar to error boundaries, right? Where uh if you have an error boundary, you try and render the real thing, right? You try and render the actual children. Uh, and then if they throw, then you fall back to some error view or placeholder. Um, it's a very similar thing here, where you try and render this one async thing, uh, and if you have enough time, then you eventually you do render it. But if it takes too long, then you fall back to this uh loading view or placeholder, and you show that instead. Uh, a few differences, one is that of course there's this element of time, so like there's a configurable threshold before you you fall back. And then the other is that like within the async case, eventually you can automatically switch back to the real view once the promise resolves. Whereas in the error boundary case, there is no like promise or whatever that resolves and then you switch back. Like you have to have some imperative action to um to trigger that.
[00:21:13] Michael Jackson: Yeah, it it you know it makes sense that uh that it kind of feels like the error boundary API because Essentially, they're both they're kind of both catching something, right? The error boundary is catching errors that you probably didn't intend, and the asynchronous boundary is catching these uh these asynchronous sort of read, or you know, these asynchronous I/O operations. Um, so it kind of makes sense that there's kind of a parallel there.
[00:21:41] Andrew Clark: Yeah, I mean, internally, they're implemented on the exact same Oh, awesome. Yeah, so in fact, in React 16.3, which was just released last week, um, you shouldn't notice any differences, but we rewrote like all of error handling in preparation for this um feature.
[00:22:00] Michael Jackson: Tell me how uh how you uh actually managed to— uh how you actually managed to do that. Yeah, yeah, how do you how do you manage to rewrite I don't know, how many how many how many lines of code are we talking about here? On the in the order of probably several hundred at least.
[00:22:18] Andrew Clark: Oh my goodness. Let me see. I can just tell you real quick. Let's see.
[00:22:22] Michael Jackson: I I mean, well, because every time every time I do sort of refactoring of that scale, um, I'm I'm basically just at the at the complete mercy of my my tests to to know whether or not I've actually broken anything. That somebody might somebody might be relying on, right?
[00:22:42] Andrew Clark: So we have a few strategies for that. Um okay, I just checked and and when I the PR that I wrote to like rewrite error handling was added 1,000 lines and deleted 800 lines. There you go. Um some of uh some of that delta was for or a lot of that delta was tests. So I actually think it made the overall it made it smaller bundle size-wise, but some of that delta was tests. Um yeah, so a few things. One is we just have really good tests coverage in the React repo at this point. Um, because recently we rewrote not just AirHandling, we rewrote all of React, right? This was the Fiber project. Um, so, uh, even before that, I would say we had pretty good tests, but, uh, especially like once you rewrite your entire framework, or once you write your entire framework twice, you end up having just a lot of good test coverage because, um, you figure out the areas that, um, were inherent, the things that were accidental. Um, the the things that uh um were most likely to create um problems in the future. Um, so we just already had a bunch of tests, and specifically a lot of error error handling tests, because we had two completely different implementations. Before the existing one, we had two completely different error handling implementations, um, that we had already written a bunch of edge cases for. So, um, that's one thing. Another thing is that uh Sometimes we screw up. Like, that's another thing. Like, after I merged this PR, we found, and even today, right before I got on this call, I pushed a fix for something that we found. So, you know, sometimes we screw up and there is a bug. Um, usually they're not like, usually by the time we ship, they're not like, oh my goodness, the entire feature is wrong. It's usually just like a silly oversight or whatever, so, which isn't a huge deal. Um, and then, uh, you know, we we've just gotten more clever, I suppose, in our testing strategies. One thing I've come to rely on more, although I didn't in the error handling case, is uh fuzz testing. Oh yeah. I use this a lot for, for scheduling. Anytime I, I deal with scheduling things, um, uh, like async rendering, async rendering, the test, unit testing is actually quite hard because, um, things aren't always deterministic, right? Like it can depend on, uh, time. It can depend on like the sequence of events or the, like how much time is in between when things start rendering. Um, so those cases are always kind of the most frustrating to Um, debug because you can't always- it's hard to sometimes create a um a reproducible failure case. And like depending on your CPU, like 100 milliseconds could be something totally different on one computer from another. So uh we've just gotten, I guess, over time we've gotten better at identifying what the possible ways things can fail and uh like proactively anticipating those. Um. But uh yeah, like I I think uh another important thing is when we write unit tests, we're very careful to write tests in such a way that they make sense regardless of the implementation. So the the way people will say this is like, don't test implementation details, um which is very easy to do when you're writing unit tests, because like at a certain point, like unit tests are inherently you're testing implementation, but um But uh we the the the trick, the easiest trick I know is if you just express your test or write your test in terms of public APIs, or where public is defined as just anything that isn't that is like consumed by people outside your team, um then you're probably good.
[00:26:13] Michael Jackson: I wholeheartedly agree. All of our uh all of our React Router tests just say uh ReactDOM.render one of our components into a node. And uh and then we just go and check what was rendered. You know, like uh and then and then you know we render we render routers, we render routes. Um but that's that's all we that's all of our tests really do. They just they just render the components that we give people and then we test the output. Um sometimes in those in those kind of rewrite situations, I was just gonna say, when you you know you know you go through a a rewrite like you did with Fiber, sometimes it feels like uh the code for the library is more of like just an implementation detail. And the tests and the documentation, like they are the library. And then uh and then like, oh yeah, this code, we have to write this code to like make all these tests pass and to make all of these things true. But in the end, you know, there's like 10 times the effort that we have to put into the uh you know the documentation and the tests than we have to actually put into the the code that that makes everything work. Do you ever feel like that about React itself in the in the React Fiber rewrite?
[00:27:29] Andrew Clark: Yeah, I should. In the tests, um yeah, of of course. Sometimes it's almost like the the code is the least important thing of the work that you're doing. Um at least, I don't know, what I I find that that's the most enjoyable type of- that's not always the case, especially on products, but I find that that's uh for me personally, that's kind of the most enjoyable work that I like to do, where it's like, um, uh, by the time you sit down to code, like the hardest part was already complete, you know, uh, where it's like a a lot of the hard work goes into just figuring out how to model these problems conceptually. And then when you sit down to code, everything just kind of falls into place. What's like the metaphor where you're a sculptor and you're just trying to chip away the the parts of the sculpture that are there. Oh, right.
[00:28:11] Michael Jackson: Yeah. Like, yeah, yeah, yeah. Like you can already There's a statue hiding in that piece of marble.
[00:28:15] Andrew Clark: I I almost feel like, um, you know, I work on a lot of branches and PRs that end up going nowhere or or like total disasters. And I find I find the process of coding itself to be very uh a very good way to like explore ideas, um, because if you sit down and you start to to implement something and you start hitting all these roadblocks and all these cases where, oh shoot, I didn't think about that, or, oh no, that actually doesn't work as elegantly as I thought it would be, uh, that's like a good indication that maybe you're idea that you had isn't as great as you thought it was is if you keep hitting all these edge cases. Um, like if if there's at a certain point, if you have all these edge cases, maybe they're not really edge cases, maybe they're just a fundamental problem in the way that you're trying to solve this problem. Um, and so a lot of stuff with React, you know, we are very conservative for a lot of reasons about making really big changes, uh, particularly to the API, um, because um, we realize how many like significant downstream effects there will be. Uh, one example I like of this is Um, the component API, uh, like the state API and the lifecycles and the classes and all that. Um, very like very minimal amount of API that uh that React has that um intentionally, like there've been a lot of ideas thrown around about like, well, what if React had a more functional API or what if React, you know, uh supported this pattern or that pattern that people are doing in user space. Um, and it's the thing is like we think about all those things, but we're And we're even aware of a lot of the problems that we that exist with the Class API, as you might have seen in Brian's post from last week about some of these lifecycle methods that we're finally getting around to deprecating. We've known about those problems for a while, but it's not like last year it would have been it wouldn't have been responsible for us last year to go out and deprecate all those methods until we figured out a vision for what we actually want to move people to. Um, sorry, I give very rambling answers. No, no, that's fine. But yeah, your your point about Your point about like the code becoming almost less important than these other things, I think those other things are like this conceptual model of how um React works. Because like once you figure that stuff out, then you can take the the those ideas and apply them to like pretty much any language, any platform, any um uh any library really, and they are just as valid. So it's like the code is um like temporary, the ideas are what persists.
[00:30:38] Michael Jackson: I like how you said that, the code is temporary, the ideas are are what persists. And the code can be scrapped and rewritten, but as long as the ideas uh are sort of advancing and moving forward, then we're then we're moving forward as a whole.
[00:30:51] Andrew Clark: Yeah, Sebastien has this really cool repo that he made, I think it was like 2016, like right around the time he was starting like the first commits on what became React Fiber. He has this repo called React Basic, I believe. And uh I think it's a cool document for anyone to read. Uh and what it is, is it's uh in Sebastian's own words, like a description of the conceptual model of React, um as at least as Sebastian saw it at the time. Uh I've read it recently and it still seems to hold up pretty well. Um and uh I remember reading that and I didn't understand everything. Completely the first time. But um I think it's really useful to have like a conceptual model that is stripped from any implementation implementation details or code that you can go back to over and over again, because it helps you clarify like what things are inherent to what the problem you're trying to solve and what things just like kind of accidentally happened over the years as you like uh made this mistake or added this edge case or-
[00:31:53] Michael Jackson: Yeah, I hadn't actually seen this React Basic repo right now, but I'll definitely include it in the show notes. It's it's interesting. He's uh He's uh kind of outlined, I guess, a lot of the different principles uh that that kind of including, I guess, in uh you know in the in the actual implementation of React or or sorts of problems, I guess, that that you want to address in React itself, which is really cool. Um so so let me uh let's just touch on one more uh subject uh because I know you're super, super busy. But you've talked about Fiber a few times. And um that, in my mind, is actually sort of a a precursor to the Suspense stuff, or what most people think of as when they talk about sort of async React. I think what we're talking about specifically is this uh Suspense API. Um but Fiber itself was kind of uh kind of a precursor, right? I'm in fact I don't even I don't even know if the Suspense API or if any of the Suspense work would have been possible uh without Fiber. Because as I understand it, Fiber was basically a a reimplementation of, as you said, basically a rewrite of the entire React core library, um which kind of gave React its own stack internally, right. It's uh it's almost like uh React has its own stack frames now and it can choose to say, uh you know, for example, in the case of the Suspense API, oh, there was a throw, okay, let's unwind the stack. Uh okay, here's a here's a boundary, an asynchronous boundary, right, let's let's catch it and let's stop the unwinding. Um is that is that accurate? Would any of the would any of the uh async or Suspense work have even been possible on the you know, non sort of pre-Fiber React architecture?
[00:33:52] Andrew Clark: You don't necessarily need to maintain your own like stack that's not the JS stack in order to do async. In fact, you could do um you could do a Suspense API like totally in sync mode, for instance. Um in fact, like it it that was one of the like design principles that we had when we were thinking of how it should work, is that uh it should work even if Yeah, you know, someone needs like one a particular update to be synced. Um, and uh, you know, and that what happens in that case is just that the placeholders with the the delay millisecond prop, um, it's as if those were just all set to zero, you know. Um, so it just immediately falls back to the placeholder instead of like waiting for some threshold. And if they're not synced, then it kind of just looks like it does today where you immediately see a spinner and it's not great, but it still works. Like the API is still works in either sync or async, you just get a slightly worse user experience.
[00:34:48] Michael Jackson: So React Fiber wasn't a necessary precursor to to the async stuff?
[00:34:54] Andrew Clark: Uh well, so uh I guess what I want to say is um you don't necessarily need async for Suspense, it's just async makes it a lot nicer, right? And you don't necessarily need Fiber for asynchronous rendering. There are like other React-like frameworks out there that use different, slightly different models for how to do async, like um uh Litho on Android uses kind of like a- it has async. A lot of these like native frameworks that do async, because the native frameworks are just inherently, I guess, have always been better at async because they have a little bit more freedom because they have different threads and they have different constraints than the web does, where everything is basically just on one big thread. So like these other React clones for Android and iOS have had different async models for a while. React Native. Uh, which is React, right, it has a completely different threading model than ReactDOM does, for instance. Um, so anyway, the the point is uh just that there's not only there's not just one way to do async. Like Fiber is probably very heavily uh in uh influenced by the constraints of the DOM and the fact that we kind of need cooperative multitasking uh and in order to uh achieve in order to get anything done on the DOM and do async rendering on the DOM today. Um, so, but, uh, yeah, so, to answer, to go back to actually answer your question, uh, Fiber, the whole point of Fiber was to do async rendering and to uh solve the problem, um, originally to solve the problem of like too much CPU work. Uh, if you have like a bunch of CPU work, you don't want it to block the main thread, uh, and you get some concurrency so that like, uh, if you have a a large React, low priority React update, it doesn't end up blocking um some higher priority thing. So basically the first part of Dan's presentation where he showed the like the little radar thing. That that was kind of the original main problem space that we were focusing on. But Jordan Jordan walk, who created React, um his uh Fiber was based very heavily on a demo that Jordan created um called uh React Metal. And it wasn't it was totally just like experimental. It wasn't uh written with the intent of like replacing React. Uh it was like written in, I think it was written in like OCaml or something and it was. Like it didn't even use the DOM and it like, it was like, uh, yeah, it was like, it was very like low, uh, I think it rendered to GL or something. Um, but uh, he he built a prototype that was very similar in a lot of ways to what would eventually become Fiber. And the whole idea was like, uh, was to solve this CPU-bound work case. But Jordan, at the time, he had this intuition that like, if you solved all these problems for CPU, For the CPU-bound case, they would probably also end up translating to the I/O-bound case. So that's why Dan in his presentation had this uh had that slide where he's saying like, you know, these are really all the same problem. If you solve it for one, uh you can map the same solution onto the other. Um so uh we had this idea for some what would eventually become Suspense. We had this abstract idea of of kind of how it should work like, you know, a year and a half ago. We didn't know all the details, but we knew something that like we want to be able to block React from committing until like everything is resolved. Um, there were a ton of like unanswered questions, um, and it wasn't really until last like December that the last piece kind of snapped into place. Um, but we had the general idea of how it should work. Uh, and then, you know, uh, we got to a point in the last few months where we had enough uh of a full story that we could show it off. So that's uh, and we're get, I think we're getting pretty close to being able to, now that 16.3 is out, to getting some version of this out to, you know, uh, products developers and library authors and people at Facebook and people externally.
[00:38:50] Michael Jackson: Yeah, that's awesome. So so last question of mine before we go, how how uh how long do you think it's gonna be before you can, for example, flip the async mode? Uh so so first question, I guess, first part of this would be, are you using async mode in any places on facebook.com yet? And then uh and then the second kind of follow-up, I guess, would be how long or or what what is required, I guess, uh until it will be the the default? What needs to happen?
[00:39:23] Andrew Clark: So we're using async mode. We've been running, like on our team, we've been running Facebook uh parts of Facebook in async mode for a while. Um but it hasn't it's not shipped anywhere publicly yet. The first uh surface or the first product that will use it publicly uh probably will be um the comments interface underneath all of the posts and newsfeed. Um that's the project that the React Data team is working on. So we're working very We're working closely with them to roll that out. Um, and uh kind of the goal of that project from the React core team perspective is just to um ensure that all of our uh assumptions and all of our uh work that we've put into async actually you know works in a real product. Um, and once we can prove that, then we'll have more confidence to you know make whatever changes we need uh and then uh you know ship it in an actual open source release. So that's kind of a lot of that work that we've already done was. What turned into 16.3, right? So that's why we have strict mode, um that's why we have uh these new lifecycle methods, that's why we deprecated some of them, is because um as we've been shipping this internally, uh you know we came across those problems. So we're like, okay, we came across these problems, everyone else is gonna come across the same problems, so let's uh let's make sure that they have a path to um to fix them.
[00:40:38] Michael Jackson: So is the point of the strict mode, uh that if I throw a strict mode in my app and everything works with no warnings, then I should be able to turn on async and expect everything to work the exact same way?
[00:40:53] Andrew Clark: That's ideally, that's the idea. I say it slightly differently, which is that if you do have warnings in strict mode, you probably will have async bugs. But if you don't have warnings in strict mode, you might still have some bugs, but you can be more confident. Uh, because we can't catch everything, right? Um, the biggest one we can't catch is uh mutation. Uh, if you mutate things, like JavaScript is just, you know, Wild West, you can do whatever you want. So there's no, we can't detect every bad or possibly bad pattern. Um, so we are just gonna do our best to warn you about the big ones and the ones that we can detect. But yes, uh, the, what you said is, is, is basically it, the idea is with strict mode is that you can opt a subtree into this, uh, stricter, uh, rendering mode. And then we'll just give you some warnings, some helpful warnings to tell you like, okay, this thing you're doing here. Probably is gonna cause problems down the road. So when you have the opportunity, please upgrade it. And the reason it's not just like a global flag and then we do it with this special um strict mode component is because uh you know you don't wanna you don't necessarily wanna have to do this for every single component in your entire app all at once. That could be very overwhelming. And if you have like you know 1,000 warnings in your console, it's not very helpful, right?
[00:42:02] Michael Jackson: Yeah, I wanna I wanna work on this little tree right now. Get all the warnings out, okay, that's good. And then maybe I could even turn on async mode just in that branch?
[00:42:13] Andrew Clark: Yeah. So that's that's this is going to be our strategy for async mode as well. We have another component which we haven't released yet. It's under an unstable flag if you really want to be, you know, throw caution to the wind. Don't do it in production though, please. Um, uh, although I think uh Zeit is actually doing it already, but that's because like, you know, uh Guillermo's, you know, on the cutting edge or whatever. Um, but uh yeah, so this will be our strategy for async as well. We'll have this thing called async mode and everything nested inside it will be uh will use the asynchronous features and everything outside of it will still be uh synchronous. So that's our strategy. Um, we've started to kind of use this at Facebook and hopefully that that works out for everyone else as well.
[00:42:52] Michael Jackson: Oh man, components to the rescue.
[00:42:54] Andrew Clark: Yeah.
[00:42:54] Michael Jackson: It's awesome.
[00:42:55] Andrew Clark: Yeah.
[00:42:56] Michael Jackson: It's so awesome, right? It's just like, here, let's have a let's have a component for. I mean, the components for error boundaries, components for possibly, you know, asynchronous boundaries, components for strict mode, components for, uh, for, you know, enabling async mode. I mean, that's- oh, I love it. I love it. Building your app in like little pieces.
[00:43:14] Andrew Clark: Yeah, composing stuff is fun.
[00:43:16] Michael Jackson: Well, hey, everybody. Uh, so let me let me let me let me ask you one one more question, Andrew. If there's anything that you want to kind of- anybody you want to give a shout out to, any projects that you want to bring any attention to for the sake of the listeners of the React Podcast. Anything that you uh that you think uh just doesn't get enough attention? Anybody I should be having on this show?
[00:43:36] Andrew Clark: Uh well, I think that it'd be cool if you just, you know, went through the whole React team. That would be cool. Um especially, uh I think uh Flarnia. I think it'd be cool to get Flarnia. I haven't asked her yet, but-
[00:43:47] Michael Jackson: She's one of the few people that I haven't actually met. I've I've tried, to be fair, I've tried to get some of your uh some of your other teammates on on the show, but But they uh you know they they have varying, varying uh I guess, degrees of comfort with being on a being on a podcast.
[00:44:05] Andrew Clark: Well, there are also some, you know, we're kind of busy. So yeah, I've resisted being on any podcast for a while just because I wanted to like ship this stuff first. Um so like I haven't done any conference talks or anything in in months. But uh yeah, but like we're getting close enough and people, you know, cat's already out of the bag with Dan's presentation. So, you know, yeah, let's just talk about it. It'd be fun. Um but yeah, I think Flarni would be a good one. She's she does. A lot of work on React, um, but it isn't quite as like visible all the time, you know. There's a lot of people here at Facebook and on the React team that do doing some really cool work and not all of it gets like into the tweets and the blog posts and the and the uh you know the public-facing stuff. So I think it'd be cool to have Flarni on.
[00:44:44] Michael Jackson: Flarni, actually, didn't she head up the um the rewrite of reactjs.org? Or the the not rewrite, but the—
[00:44:51] Andrew Clark: Oh, uh she worked on that. I think that's primarily Brian who worked on on that uh on the that stuff. Brian would be another cool person to have on on the podcast. Flowny is doing a lot of the the Relay integration stuff and a lot of the like the hard work to um like take async mode and figure and like you know figure out how we can improve it um on actual products at Facebook. So she's a really cool person.
[00:45:15] Michael Jackson: Well, thank you so much, everybody. Andrew Clark, ACDL. @acdelight on Twitter and and on GitHub, I believe, @acdelight. Um, and thank you so much for joining us today. And I'm sure I will see you around soon.
chantastic.fm