chantastic.fm

29: Don't Rewrite Your App for Hooks and Suspense with Jared Palmer

Episode 29 · November 20, 2018 · 00:47:43

Hosted by chantastic · With Jared Palmer

Download MP3 · Jump to transcript

Jared Palmer is a passionate JavaScript developer, pushing developer ergonomics in React with projects like formik and react-fns. Chantastic asks about what Suspense and Hooks mean for existing apps and what we should know to migrate our code sanely. They discuss why doing away with render props is a good thing, why Hooks are up to the task, and how Hooks and Suspense will impact libraries like formik, react-fns, and the-platform.

Featuring

Links

React Holiday

Season two starts December 1st.
Signup here!

Transcript

[00:00:00] Michael Chan: This is React Podcast. I'm Chantastic. In this episode today, I'm joined by Jared Palmer. I am thrilled to share this call because Jared and I share a relentless pragmatism. And it is so fun to hear him talk from his perspective about all the new cool hotness in React. Despite his own excitement, he cautions no less than 3 times not to rewrite your app with hooks and Suspense, but instead gives us tools and knowledge to take a measured approach in updating our apps over time. If you're excited about all the new things announced at React Conf 2018, but aren't quite sure what they mean for you and how you can take advantage of them, this episode is for you. We'll jump right in, but first, a word from our sponsor. This episode of React Podcast is brought to you by React Training. In-person, hands-on training for development teams. Taught by community leaders and experts. For more information, visit reacttraining.com. And stick around for the end of the episode. If you're just getting your feet wet in React, I have something that I think you'll like. Jared, thanks so much for sitting with me. I appreciate it.

[00:01:28] Jared Palmer: Happy to be here.

[00:01:29] Michael Chan: Yeah, so we're uh we're sitting here. This is the night before React Conf 2018 is when this is being recorded. Who knows when you're hearing it, but uh we're gonna talk about— we're gonna talk about the new stuff in 16.6 and some stuff that uh we know is getting announced tomorrow, and you'll know about by the time this airs.

[00:01:48] Michael Chan: So um Yeah, how are you feeling the night before?

[00:01:51] Jared Palmer: I'm very excited. Just reading the the schedule, I'm like very excited for a lot of for a lot of the talks. There's gonna be some cool stuff tomorrow.

[00:01:56] Michael Chan: What are you most excited about?

[00:01:57] Jared Palmer: I mean, the elephant in the room I think is like what like everybody's gonna be pumped about is our hooks. Like I think it's gonna be really, really cool. Components have always been a problem. Like if you've ever given a workshop, like we give workshops, and just like ES6 is quirky still, and this is takes a full essay to explain in JavaScript. So that's like part one, I'd say like hooks, like there are two things for me that like really are exciting about hooks. First is like getting rid of this.

[00:02:24] Michael Chan: Yeah.

[00:02:25] Jared Palmer: The second is they fundamentally replace render props.

[00:02:31] Michael Chan: Yeah.

[00:02:32] Jared Palmer: So with that, and render props are great. I have like one of the more popular render props libraries. I think I have like, I guess, well, I don't know. It's up there.

[00:02:40] Michael Chan: Which is React Functions?

[00:02:42] Jared Palmer: React Functions. I didn't even think about that. I was thinking about Formik, but sure. I have like, maybe, maybe I have like top 10. I have no idea, but like, I was just thinking about Formik, but Formik is like fundamentally about the render prop. Like the whole- it was about like a higher-order component at first. And then once I saw the light and saw the render props light, I was like, oh man, this is great. I should just make both. So Formic does both. And like, it's worth the wait. And I know people that come up to me after conferences and stuff and they're like, I love the higher-order component. It matches our functional style. And I'm like, that's cool. I don't use it, but that's awesome. I'm so happy to support you. Right?

[00:03:16] Michael Chan: Yeah.

[00:03:16] Jared Palmer: But I use render props and I think render props are great. We use them for all kinds of things on that stuff that we do with GraphQL. We'll use, you know, the query component from Apollo. But we also have like, we'll write our own query components for REST and just like do that. And like, I've thought about writing a caching thing for REST like 20 times with query components and stuff. But anyway, it doesn't matter. Like we use them all the time. The problem is though, is that like you end up having, especially with like Prettier, you end up getting this situation at least like 2 times a day where you're just like, refactoring some render prop, you're in some big component, yeah, and nothing works. Yeah, and you just delete it. You like, you like use- for me, I like love Alfred and like clipboard history. So I'm just like clipboard history, clipboard history. I gotta figure out where this was. Apple Z 17 times. Things break. Everyone's been there. And you're just sad. Yeah, you just forget a paren. And you can never figure out where exactly that map is supposed to end. Yeah, it's like it's always inline map and it just blows up your whole like couple minutes, and like if someone interrupts you during this time, like just sadness ensues.

[00:04:16] Michael Chan: Yeah.

[00:04:17] Jared Palmer: So render props are great, but this call- you still suffer from callback hell, like the classic Node issue, right? Like keep pushing it further and further. Yeah, just keeps going further and further into the editor. And I've seen some crazy stuff like- Yeah.

[00:04:27] Michael Chan: Oh yeah.

[00:04:28] Jared Palmer: And

[00:04:29] Michael Chan: I love seeing screenshots of that. Like every once in a while someone will put a screenshot on Twitter. Like, dang, that is the deepest one I've ever seen.

[00:04:36] Jared Palmer: And look, there have been a lot of attempts to solve this. I think I saw one like React Adapt where they like- someone's tried to like chain render props, which are cool and interesting. But those- and I think I've even seen one like a Babel transform or some macros. I don't know. Anyways.

[00:04:48] Michael Chan: James Kyle kind of did a screenshot of one, right? And he used kind of like an await style syntax.

[00:04:53] Jared Palmer: Hooks takes all this and just is like, you know what, this is like the wrong way to do this. Let's just build this as a primitive. What's that primitive? Oh, a function. And just a function. And what's so great about hooks are it's just JavaScript. And so I think what's interesting about hooks, like, is that they render- they make render props basically like an antiquated pattern, but at the same time, you can really easily build render props out of hooks and make things backwards compatible. So if you want to support both, you totally can. And so like, hint, hint, wink, wink, on Friday, you'll see some stuff for React Functions that like will have hooks in it. Cause my plan is to rewrite the whole thing almost. Well, maybe by the end of the weekend, it'll be like some hooks.

[00:05:34] Michael Chan: Awesome.

[00:05:35] Jared Palmer: So that'll be fun and and and to show there. So yeah, I think hooks are really exciting. I think they're gonna change people's React has always been unashamedly JavaScript first. And that's always been like kind of a criticism. And so people who come from the HTML CSS land, they're, they, they lean to Vue sometimes because it is just like HTML CSS and it makes more sense, right. To them. What's interesting about this, like transition to React is that now we're going like all in on JavaScript. There's like, JSX is great. It's still, it's not going anywhere, obviously. Maybe they'll solve that next, but like now we're- everything can just be a function.

[00:06:10] Michael Chan: Yeah.

[00:06:10] Jared Palmer: And that's beautiful. And so hooks are really easy to test. Everything's in scope. They're easy to refactor. I think that's also maybe the thing that is like reading through things. I think that maybe the best part is that like, because you're defining state, not in this like object, you're defining it like as one- well, basically like a tuple effectively in your little editor. And you got multiple functions in one file. You can just like move that state line up.

[00:06:33] Michael Chan: Yes.

[00:06:34] Jared Palmer: And move it back down.

[00:06:35] Michael Chan: Yeah.

[00:06:35] Jared Palmer: And move it back up and move it back down. And that's, that's very powerful.

[00:06:39] Michael Chan: Yeah.

[00:06:39] Jared Palmer: You have the state, you have this piece of state and the updater right there. Also, okay. Number 3, we use TypeScript. And so we love TypeScript. I live in a TypeScript universe and it's phenomenal. You should join if you're not already.

[00:06:51] Michael Chan: Oh, I love it. No, I'm in there with you. Yeah.

[00:06:54] Jared Palmer: So we live in a TypeScript universe. We use TypeScript. So if you, if you use React and TypeScript, which by the way is pretty great situation. When you go to create a component, a class component, you're gonna create two interfaces usually with state. You're gonna have your interface something something props and interface something something state. And those are gonna be the generics that you have to pass to the component to describe the props and the state that need to be passed into your component to work.

[00:07:20] Michael Chan: Yeah.

[00:07:20] Jared Palmer: Flow is like sometimes a little bit smarter, just tries to be- tries to do this inferencing, but TypeScript is beautifully not good at inferencing. But that's actually a feature, not a bug.

[00:07:30] Michael Chan: Sure.

[00:07:30] Jared Palmer: So, right? So what's cool about hooks though is you don't need to type this extra interface for state all the time. So TypeScript has tuples and so does Flow. And so when you return a tuple, you can just basically from, let's say, a hook or something like that, like it can get inferred. And so useState can infer the type of the initial state And so you don't actually have to type out your state necessarily.

[00:07:54] Michael Chan: That's awesome.

[00:07:55] Jared Palmer: You might need to, if you're gonna set initial state to something like null, but it's gonna be something else. Right. But that you'll use a generic there, but like there's gonna be a lot of type inferencing that can occur. So if you're a TypeScript person and you're always like, and they say like, I don't know, I read some Medium article once that like TypeScript's like 2.2x more code than some, I don't know. It's just like more code than like Flow, which is more than, and it doesn't matter anyways. It's less code. That's what you need to know. It's less code to type. It's gonna be faster and there's more inferencing and that's awesome. Cause moving around state is annoying and so is moving around your interface types, but you're gonna get great autocomplete and leverage TypeScript tuples probably. So yeah, I mean, for those 3 reasons you have like that, I think hooks are gonna be like game changing. Yeah.

[00:08:39] Michael Chan: This, you know, so this kind of goes the opposite direction of what, what you were just saying. But one thing that I found really fun is, is that Um, you were talking earlier about classes and how they can be problematic. And I remember that moment when we kind of moved away from createClass into class, and it was- it really forced people to kind of like double down on their, like, on Babel or whatever their pipeline is.

[00:09:02] Jared Palmer: Yeah.

[00:09:02] Michael Chan: And something that's really cool about hooks that I enjoyed when playing with them is, is that you can write hooks that are just like old school JavaScript. You use that function syntax. You don't have to transpile those or anything. And you're like good to go.

[00:09:15] Jared Palmer: You can definitely get much- well, browsers I think have caught up slightly. But like if you were to take what this API and put it back, like let's say 4 years ago or something like that, like, uh, it's questionable if the like whole Babel webpack thing takes off the same way. Cause like, who knows? You don't need that driving force of we need to transpile, we need to transpile all the time. I mean, you still like JSX is still like amazing and great, but But

[00:09:38] Michael Chan: you could do it without it.

[00:09:39] Jared Palmer: You could do it without it. Like maybe a little widget here and there. Like, that's a great point. You know, it is just JavaScript, which is great. But I also- and I'm not worried, I'm just like about the learning curve a little bit.

[00:09:51] Michael Chan: Yeah. About the hooks learning curve.

[00:09:53] Jared Palmer: Well, it's so- anything that's new is gonna- you're gonna like adversely react to immediately. And the first time I think I saw hooks, like, I was like, what is this? Yeah. Uh, no. Well,

[00:10:02] Michael Chan: there's that. Like, you feel like you're losing control. Because like, I understand class, I understand the instances, I understand Like the way this kind of cascades the relationships here and like that's being taken away from me and like, what do I get? Yeah.

[00:10:13] Jared Palmer: What do I get? So, so, but then I slept on it and then I played with it a little bit and I was like, okay, you know, hmm, maybe this is kind of nice.

[00:10:23] Michael Chan: What's that same thing as, uh, I remember early on really having this reaction to setting your onclick on an element. Cause for 10 years people had told me that that was like bad, like writing that in the JSX. And it felt so weird at first. And then I was realizing, oh, hey, like it's doing the work of kind of uh hooking it up the way that I would have had to do it uh myself. And it's being, it's being smart about it and I can trust it to do that work. And like, after you play with hooks for about like a day or two, you kind of go like, oh, I can trust React to do this work as well.

[00:10:56] Jared Palmer: Yeah. And I think, I think my, my, my thing was like, there, there are definitely, you definitely need to read the docs. So if you haven't read the docs on hooks, go read the whole docs and read the FAQ.

[00:11:05] Michael Chan: Start to finish, all the way through.

[00:11:06] Jared Palmer: Top to bottom. Don't skip a section. Do not, because you will learn things that you didn't. Anyways, just like React is like gotchas, Hooks have gotchas too. Like there's no silver bullet here. Like you still need to remember a bunch of stuff, but it seems a little more ergonomic and it seems, it almost seems like just easy to refactor, easier to refactor.

[00:11:27] Michael Chan: Yeah.

[00:11:27] Jared Palmer: I think that's my biggest thing. And like that pain of like oh no, I need state. Like that, that, that is a painful moment. Uh, she said, where I think my snippets now, like for, I gotta change them, but like, I think they only, they, they go straight to component at this point, but now maybe I will not do that. So I gotta change those.

[00:11:45] Michael Chan: Yeah. I really want to uh kind of build up like my next big feature using just function components and hooks. I think it'd be really fun.

[00:11:53] Jared Palmer: And suspense.

[00:11:53] Michael Chan: And suspense. So that brings me back to-

[00:11:55] Jared Palmer: You should get the suspense probably, right?

[00:11:57] Michael Chan: That's a great idea.

[00:11:58] Jared Palmer: Although, although, although I will say last bit, last bit on hooks. What's cool about it is like the one One that like the first hook I built, sorry, we built, I should say, is like that, this like list hook, useList.

[00:12:07] Michael Chan: Okay.

[00:12:08] Jared Palmer: So Formik has list manipulations. So like there's a component called Field Array, which basically saves you enormous amount of time. Cause like, what are you gonna do about uh an array? You're gonna add, remove, insert, like all this other stuff. Right. Like, and no one wants to go look those up on Stack Overflow. Like, let's just do that.

[00:12:23] Michael Chan: Or you like break it in half.

[00:12:25] Jared Palmer: Break in half, insert, all the things. Right. So like, basically that was like the first, that was the first hook that I thought was like, oh, this is nice. Like, okay, we can like make state paradigms, not just like, not just like, like, okay, obviously browser APIs, that stuff's like super obvious, but like weird stuff, like use normalization.

[00:12:43] Michael Chan: Yeah.

[00:12:44] Jared Palmer: Or like, I don't know, just there's other types of data structures that could be really like, or like use a state machine.

[00:12:51] Michael Chan: Yeah.

[00:12:52] Jared Palmer: Or that, like these kinds of things can are now possible and they're possible before, obviously, like you could do that in a render prop, but like. I don't know. I don't think people were like, as I think people are gonna, cause it's just a function. I think they're gonna approach what's possible potentially differently. Like I would think it was like a code smell to see like use array or like array in JSX potentially. Like that would be like kind of strange. I think the future is very, very bright for, for, for hooks and where this is all going. And it's like very exciting to see this all come together. And it's also nice to see like how the paradigm of render props has kind of evolved into the framework.

[00:13:24] Michael Chan: Yeah.

[00:13:24] Jared Palmer: And embraced, started like with some- started with like React Motion and then like got more popular and then brought in, now it's context. And now it's like, this seems like the next logical step.

[00:13:33] Michael Chan: Yeah, absolutely. Um, so one thing that I've been loving is that with hooks and with Suspense, um, now a lot more happens in that render method.

[00:13:43] Jared Palmer: Yeah.

[00:13:43] Michael Chan: And so like, kind of like before you returned the thing that you're returning, you kind of just line up now all of the things that you need. So I need this piece of data, this piece of internal state, something from the browser and this thing from our API and then use it.

[00:13:57] Jared Palmer: Yeah. So my initial talk proposal for React Conf was I'm going to Render land. Like that was, that was like the- I think that's what I titled it, or that was like one of my submissions, but that was like on some esoteric API that didn't actually end up getting- it doesn't matter. But like, that was my idea. It was just like, yeah, like we're moving to Render and you know what's cool about Render? It reads top to bottom.

[00:14:18] Michael Chan: Yep.

[00:14:18] Jared Palmer: And so like if you ask like your grandmother, like, and you describe to her like how code works, the first inclination is that it works like languages work, where you read top to bottom, right? And JavaScript doesn't work that way.

[00:14:31] Michael Chan: No.

[00:14:31] Jared Palmer: And just explaining like closures and callbacks and scopes is very confusing. But now by pushing things down to render, things read top to bottom.

[00:14:41] Michael Chan: Yeah.

[00:14:42] Jared Palmer: Like, that's pretty cool. So I think that's also extremely powerful because like readability is important for maintenance and also refactoring, that kind of movement of state back and forth. So I think that that's a big deal that like we're moving a lot of logic to render. And it's funny because like moving it to render is really just like moving it into functions. If you think about it.

[00:14:59] Michael Chan: Yeah.

[00:15:00] Jared Palmer: It's just like more functions, less, less imperative code. More like if it's in render, it's declarative by default, which is- they're sneaking that up on us, right? Like They started with that ternary stuff.

[00:15:10] Michael Chan: Yep.

[00:15:11] Jared Palmer: Right. They started with the maps and filled in like, no, I hadn't written a map before that. Like,

[00:15:16] Michael Chan: oh yeah, no, no, no. That was my first map.

[00:15:17] Jared Palmer: That was, that was like everyone's first map. Like no one, like I knew map, I knew about it, but like no one had, no one's honestly written a map before React forces, forces us all to write maps.

[00:15:26] Michael Chan: Yeah. Yeah.

[00:15:27] Jared Palmer: I mean, it's just not something you did in the jQuery universe.

[00:15:29] Michael Chan: Oh yeah. I remember reading the docs and for the first time and being like, that's a, like, that's a function in JavaScript. Like I didn't even know that it existed.

[00:15:37] Jared Palmer: Yeah. I'm

[00:15:38] Michael Chan: like

[00:15:39] Jared Palmer: 100%, like not even embarrassed to say, like, I don't think I wrote the- I don't think I used .map before React. So, um, yeah, but now I use it every day. So they're sneaking up this functional stuff and it's like declarative and it's been, it's been working and it's great. And I'm really excited about that, but yeah. So coupled with Suspense though, which is what my talk about- my talk is about, it's gonna be like bananas.

[00:16:02] Michael Chan: So tell us a little bit more about Suspense. What's the high level on Suspense?

[00:16:06] Jared Palmer: So React Suspense is about the efficient loading of resources. Or should I say orchestrating the efficient loading of resources. I might need to re-record that because like legitimately I have it written down, but I don't even have to memorize it. So, but basically it's blank. We're gonna continue on. The problem that most of us suffer from, and at least I deal with this like multiple times a day, is I write the word like isLoading, I don't know, 17 times a day. Because that's how you load data. The way you load data in React is on the client, didMount, you go fetch some JSON. If it's there, you just tell it's no longer loading. You set state to isLoading false and you put it up into state. And then in your render function, you go, is it still loading? No. Does it exist? Maybe. Are you sure it exists? Yes. Okay. Then we'll render the thing.

[00:16:58] Michael Chan: Yep.

[00:16:58] Jared Palmer: And that's not great for two reasons. Um, if you're trying to optimize for like a really fast application, make your application fast and speedy and responsive, and you want to have it be great on all network conditions, and you want to deliver a great user experience, these two concepts, like speed and performance and user experience directly conflict with one another uh when you have this kind of loading state. Let me explain why. User experience research shows that users don't like janky UIs. They don't like seeing loaders that immediately go away.

[00:17:35] Michael Chan: Yeah.

[00:17:35] Jared Palmer: They actually think an application like doesn't work as well if that happens. So perceived speed happens at like some sort of threshold around- I don't know what it is. It's like a 400, 300 milliseconds, or even 200 milliseconds, where a user doesn't even perceive that the application is in some sort of loading or interim state.

[00:17:55] Michael Chan: Sure.

[00:17:55] Jared Palmer: And so in a perfect world on fast connections, you don't even render a loader. Okay, but on slow connections, user experience research also shows that users want their apps to be responsive. So if something is taking a while after a certain threshold, they do want to see some sort of responsive feedback. They want to see a loading indicator. They want to know that what they've just clicked is having some sort of impact. This is a conflict. This is fundamental to like the way that the universe works. Because just because if you like render null instead of a loader, that doesn't solve the problem. There's literally still loading state there. So, like, showing nothing doesn't solve the problem. And obviously, if we're trying to like be a good follower of our webpack brethren over there, you know, over on the team, and Sean Larkin, shout out, like we're going to code split We're gonna delay stuff, we're going to lazy load wherever it's possible. Lazy loading data, so that means uh modules, data, and assets like images and stuff. And there's tons of components that do that already, but they all suffer from the same problem, which is they don't react to the network at all. So what Suspense does, and this is a big buildup for this, so Suspense allows us to do is, it allows us to tell React where to suspend rendering. While other components ask for resources. Okay. So let me say that again. So the Suspense component tells React where it should pause while other components wait for resources. When those resources arrive, it can continue down the tree. Or uh after a certain duration, it can render a it can render some sort of fallback, that placeholder, that loader, and then continue down the tree. So what's cool is you can use this to orchestrate fallbacks. And delete all that loading state because it literally doesn't exist anymore.

[00:19:35] Michael Chan: Interesting.

[00:19:35] Jared Palmer: So the Suspense component makes it declarative, something that was completely- I would say it was still declarative, it's still like stateful and Reacty, but there was this ternary expression in all of your renders were about like loading, right? So instead of having this like gigantic ternary or trying to coordinate stuff, you just describe the place where React should suspend and wait, and it actually withholds any DOM mutations while this happens. And it continues down the tree. So what's cool about that is you can use this for all kinds of things. The first use case is gonna be lazy loading modules. So there's a new thing called React Lazy. So you call React.lazy and it's just purpose-built for dynamic imports. And if you put a suspend component above a React.lazy, the the result of a React.lazy, it will suspend, pause, literally pause rendering. Until that component resolves from your module chunk from like Webpack or something, and then it continues. And if that takes longer than a certain amount of time, it will render this fallback of your choice. So on fast connections, your users won't notice anything. It'll be instantaneous. Sure. And on slow connections, you'll see the fallback, which just perfectly solves the problem. And so it kind of removes an entire class of issues result relating to loading. Plus, if you use it for data, which something like an Apollo or a Relay will ultimately do, it's gonna simplify this sort of story around data fetching and and and undefined state. So, like when you're currently, as I mentioned, because you have this like ternary expression usually in render, items or whatever you're fetching might not be defined, right? But now, thanks to the way that um Suspense works and the way that this thing called React-Cache works, which is a basic cache for React that's also coming out. Everything's always defined. So if you use TypeScript, that's awesome.

[00:21:24] Michael Chan: Oh, it's amazing.

[00:21:25] Jared Palmer: It's amazing, right? You don't have any you don't have any you don't have any you don't have any these partials. Like it's defined. And you use error boundaries to control the rejection, the promise rejection. So like if you're if you have like an API error, you reject and then that is captured by your error boundary. And the idea with the error boundaries, if you're not familiar with them, is they can catch errors thrown at runtime and render UI related to those errors.

[00:21:49] Michael Chan: Yeah.

[00:21:49] Jared Palmer: So let's play pretend here that it's facebook.com and you're in the chat, you're in a little Messenger chat and your chat explodes. Okay. For some reason, literally poof, it explodes.

[00:22:00] Michael Chan: Yeah.

[00:22:00] Jared Palmer: Okay. You could render something with an error boundary around a certain area of that chat. It's like, okay, this thread just exploded. But the rest of your messenger still works. So that's pretty useful.

[00:22:09] Michael Chan: Yeah.

[00:22:09] Jared Palmer: So you're supposed to use those to control errors with Suspense.

[00:22:12] Michael Chan: Gotcha. As opposed to letting that just propagate all the way up and destroy your page.

[00:22:16] Jared Palmer: Right, exactly. So you sort of catch that there. So that's pretty cool. So it's a little different paradigm, but it's very declarative. And what's really great about it is coupled with hooks, it's like reads top to bottom. So Suspense, at least with React cache, reads synchronously. It's kind of like this sync async thing. So you describe an asynchronous resource. And it gives you back a synchronous way to access it.

[00:22:37] Michael Chan: So ergonomically, that seems really nice because the the the pattern right now, as you were talking about earlier, is you you have some kind of component and as you need to wrap it, like let's say we wrap it in some type of component that doesn't render anything, but maybe just changes the document title. Those types of imperative things get wrapped up into the components. Now we have kind of like wrapped our rendering component in this thing that has some other side effect. But now when we use those, we don't have to touch the the return at all. We just kind of put that in the body of our uh render function. But like above that that JSX return that we're returning. So we can easily compose things in and out without having to actually change the whole component.

[00:23:19] Jared Palmer: Yeah, you can do with hooks and Suspense, you can also like ensure that things are, let's say, in place before other things happen. So coordination is oftentimes tricky. That's why you have these crazy loading states like is loading thing, is loading other thing, is loading other thing, or is loading other thing, then we can render, right? You see that. And that's again, render props tries to solve some of that. And you have this waterfall issue.

[00:23:37] Michael Chan: Yeah.

[00:23:38] Jared Palmer: What's cool with with Suspense is, and with React cache, this basic cache, is it reads top to bottom. So you can guarantee that if you read from the cache on one line, that the next line, that item is always defined. So you just pass that as maybe that may be the input to the next part, next read. So you can like chain them up. And so with Suspense, and I encourage you to read the docs about it, but with Suspense, siblings of a single Suspense component fire in parallel.

[00:24:03] Michael Chan: Tell me a little bit more about that.

[00:24:04] Jared Palmer: Okay. So the best way to think about it is if you've if you're um if you have components um underneath a Suspense component that are going to suspend and they're siblings, they will be they will be like run in parallel. Mentally, that's the way to think about them.

[00:24:23] Michael Chan: Gotcha.

[00:24:24] Jared Palmer: Um, and it will wait for all of them to be completed. So think like promise.all. That's the best way to think about it.

[00:24:34] Michael Chan: Gotcha.

[00:24:35] Jared Palmer: And if you have like nested components, yeah, nested components with Suspense, they're gonna go in waterfall.

[00:24:40] Michael Chan: Okay. Now with the children, can you suspend just like another child? Like

[00:24:46] Jared Palmer: so you can keep nesting Suspense infinitely out. So one of the cool- okay. So let me give you an example. This may be more concrete. All right. So I am building a low-quality image placeholder. Very common technique. You have like a placeholder, which is like a low-resolution version of of of of an image. And you fill it in later with the high-resolution image when it downloads. 42 pixels is usually the little one. And the big one's like, you know, your gigantic hero image. Think play pretend. Okay. So-

[00:25:13] Michael Chan: And the idea is you get the first one really quick.

[00:25:14] Jared Palmer: You get the first one really quick. And then you render the big thing when it like- in the background. And what that allows you to do is you're able to show UI immediately that's close enough to the real thing on bad connections. Maybe that's like, if it's like a little avatar, like you get the sense of the person's face, but you may not, but it may be blurry. Like you could do cool stuff with it anyways. Great use case for Suspense. So you may have Suspense components all over your application and you're gonna want to have tight control over where these occur.

[00:25:36] Michael Chan: Yep.

[00:25:36] Jared Palmer: But you could build a really quick, low-quality image placeholder and swap out that like maybe pretty massive progressive image thing you've been Playing with, uh, with Suspense. And the way you do that is you take a Suspense component, you would put it- let's- you'd wrap it on top of an image component. Uh, but this image component isn't a normal image component. It's gonna be- it's gonna use something like React Cache or a cache. And when you would describe, uh, in a- in wrap in a promise, just a straight up like 2005 style image creation, like const image equals new Image image.source equals source. And like image.onerror, like you'll just reject this promise. And image.onload, you'll resolve the promise. And then that will be like, that will return a resource through React cache to you. And then you'll create a new like capital I Image component. And then that will read from the cache whatever the source is that you pass into it. This is the capital I. This is the component image. The component image. And the first line of that is a read from your image cache. So when that gets called underneath the Suspense, it will wait before returning what's next. And what's next is a normal Image component with that source.

[00:26:49] Michael Chan: Yeah.

[00:26:49] Jared Palmer: Okay. Okay. It's a little, it's a little, little weird, right?

[00:26:51] Michael Chan: Right, right.

[00:26:52] Jared Palmer: All right. So that, that's like, and so what you do is you take your capital I Image, you put it underneath a little suspen- a Suspense component and you like basically pass in the high-end version of this image. And what that's gonna do is, as I just described, it's gonna pause and wait to download all of that. But then you set a fallback of the low quality one.

[00:27:09] Michael Chan: Yeah.

[00:27:10] Jared Palmer: This is gonna get really weird. All right. I'm trying to describe this. Maybe this is too, too abstract. So you set the fallback to be the low quality one.

[00:27:15] Michael Chan: We'll have to like link a CodePen or something.

[00:27:17] Jared Palmer: Yeah. Sorry. So I have to link a CodePen, but basically like, and I'll do this on like on stage tomorrow, it'll all make a lot more sense. And like, you're gonna use this component literally on every image you write from the future. So you heard it first here. Um, you'll do the low quality one as the fallback. And so let's think about that. What we described. So we know that like Suspense will wait.

[00:27:35] Michael Chan: Mm-hmm.

[00:27:36] Jared Palmer: And then we'll fall back. And you can also set a max duration of its wait time. So let's just say, okay, let's wait for 1 second. If this big big image isn't loaded, render the fallback. So all that hoopla I just described for like 3 minutes and like didn't make any sense is literally just the perfect exact behavior you want for image placeholders. And best of all, on fast connections, it won't even render the fallback. It'll just go straight to the hero image. So like that's the ultimate user experience right there. And so that's what Suspense allows, because we can literally pause render. You can enable all kinds of crazy stuff. So like images is a good example, like loading scripts. Let's say you have to load like the Stripe SDK and you can't render your checkout button until Stripe is loaded. You can suspend rendering until this it's loaded. Another one would be like flip animations on a route transition. Yeah. So like oftentimes you want to do some crazy animation, like in the Vue community loves rubbing this in our face, just like some crazy, crazy Nuxt animation or something like that. You need to know about what the DOM node look like, what it looks like on the other side. Yeah. To run do a flip. Animation's always been kind of tricky with React in that kind of sense. It's kind of like imperative kind of code. So with Suspense and like Reach Router, Ryan's thing, you can suspend the route transition. It can wait. You can figure out what's going to be rendered ahead of time, know about it, and then do the flip animation and bring it all in because you can pause rendering in between the transition states. So like it's super technical and it will all this will all become like much more obvious hopefully tomorrow after my talk. But like it's pretty wild, like the the ability to literally stop time from happening. It's amazing. Um and it's gonna clean up so much crazy, crazy code.

[00:29:20] Michael Chan: Yeah.

[00:29:21] Jared Palmer: And then uh hopefully this will be like a better story when it comes to like perhaps even like server rendering. I think they're gonna try and make it work with server renderer too. Uh and so like that'll be cool. Because if that if if if it's done right, it's possible that like right now with server rendering, if you're not if you don't know about it, you have to like usually do like two you have to know ahead of time what um data your components need. Right? So Apollo actually has to like literally render your whole walk down your entire React tree twice. So Apollo does it once and then React does it again. So it's pretty slow.

[00:29:55] Michael Chan: Because Apollo has to like figure out what you need, actually get all that data, and then React's gonna render it out with the data once it's done.

[00:30:02] Jared Palmer: Correct. Correct. And but in the future, there might be a thing that like streams down everything and only goes through the entire tree one time.

[00:30:14] Michael Chan: That's crazy.

[00:30:16] Jared Palmer: Which is amazing. Pretty bananas. And yeah, that I mean that that's gonna be a pretty cool world. It's also gonna mean that you can add server rendering to your application like pretty trivially in the future, potentially, right? You just need to make sure you're not using like window in certain situations. But like that's what the isomorphic fetch polyfill is for, right? Like combination of Suspense and Hooks is it's gonna be like the data story. But I will say that it's not quite there yet.

[00:30:38] Michael Chan: Sure.

[00:30:39] Jared Palmer: So okay, so React Cache is coming out. But it is not ready for prime time yet, I will say. And we will see, just like in the 2016, 2015 time where we had like Flux mania, like we're gonna have Cache mania.

[00:30:54] Michael Chan: Yeah. Oh yeah, absolutely believe it. So my understanding is Suspense is baked into React. It's a, it's a component. Yeah, you're gonna have, um, but Cache is kind of like a- gonna be the Wild West for a little bit. Like, like React Cache is the starting implementation, but that's where a lot of the innovation could potentially happen over the next year.

[00:31:14] Jared Palmer: Absolutely. It's just like a reference for how it hooks into React at the moment. But yeah, like it's gonna be pretty crazy. I'll talk about this too. Like don't rewrite your app, uh, tomorrow for sure. Like don't do that. Uh, don't do it on Monday.

[00:31:29] Michael Chan: You're just asking for pain.

[00:31:29] Jared Palmer: Yeah, no, you're going- it's going to be painful. And so I would definitely just stick to like lazy loading components, some image stuff that I'll show and like some cool stuff like that. Start there. If you want to get wild, do some data stuff. But, um, yeah, like I would wait, or those of you who are ambitious, like think about how to do this better and they'll come up with some awesome way. And I'm sure there are people that are gonna release like 5 different libraries, like on Friday.

[00:31:56] Michael Chan: Oh, sure, sure, sure. I'm sure there's a ton just like waiting to like,

[00:31:59] Jared Palmer: yeah,

[00:31:59] Michael Chan: deploy.

[00:32:00] Jared Palmer: There's like people just like, was Jared's talk over? Is Jared's talk over? Jared's talk over? Okay. There it is. Boom.

[00:32:05] Michael Chan: Okay. So I want to back up just a little bit, kind of like cover the things that we talked about. And then talk about progress for people's apps, like how people can kind of move forward with this incrementally. So first of all, we're getting Suspense, uh we're getting Hooks. We can throw anything that we want into these caches. So it's not just, it's not just data. We can put components in there. We can do like these image things.

[00:32:28] Jared Palmer: Image, audio, video, scripts, probably some other stuff that I don't even know about.

[00:32:33] Michael Chan: Yeah. And then Suspense itself is not just about fetching data, but it is about that-

[00:32:38] Jared Palmer: Yeah, it's about suspending render. That's the basically the best way to think about it is about uh efficiently orchestrating resources.

[00:32:46] Michael Chan: Now, all of this seems really in line with the original view of React, which is to kind of like stop time and in doing so, be able to give people a great uh user experience. I'm remembering early on, like like Pete Hunt's talks where he was like talking about like being able to like take time out of the equation.

[00:33:04] Jared Palmer: I know what wavelength you're on. It's like React. So jQuery, there's con- if you go to jQuery application, there's a concept of time.

[00:33:09] Michael Chan: Yeah.

[00:33:09] Jared Palmer: Like you don't know necessarily what's happened or if you do something, what's it, what it is about to impact.

[00:33:15] Michael Chan: Yes.

[00:33:16] Jared Palmer: So like React solves this by removing time. So like the way I, the way I refer to this concept is like, like let's get mathy here. Go back for a second. Okay. So like React is like a function of like state and props. And so F of like state and props is like UI.

[00:33:32] Michael Chan: Yeah.

[00:33:33] Jared Palmer: Okay. But now it's really just like props cause like whatever, like, okay. But it was like F of state and props is like UI, right? But jQuery, it's like a function of state, like maybe some props and like time.

[00:33:44] Michael Chan: Time.

[00:33:45] Jared Palmer: No time in React because you're describing the way things are. I forget which one it is, but like you describe the what and React takes care of the how.

[00:33:55] Michael Chan: Okay.

[00:33:56] Jared Palmer: Or I may have just gotten that backwards. But it makes sense if you think about it. No, I don't know. Someone smarter than me wrote that. But um-

[00:34:02] Michael Chan: I've been thinking about that a lot actually uh recently. Personal anecdote, um, my my wife and I use my car a lot because it's more gas efficient, but we use the air conditioning totally differently. She likes to just jam down the air conditioning to like 62 degrees and then control as she needs, right, the like how much of the fan, right? Like she uses it very imperatively. I'm like, I'm like, 72 degrees.

[00:34:27] Jared Palmer: That's right. That's a great- that's a great- I don't care what you do. I don't care how you use it. Right, but the problem- so here's a good counterexample of that. The problem with the declared at 72 degrees is if you don't have like a Nest, what you'll have is an air conditioning that like goes up to like 74 degrees and like blasts it on high.

[00:34:45] Michael Chan: Yeah. Yep.

[00:34:46] Jared Palmer: Until it gets down to 72 and then stops, and then it goes back up to 76 and it blasts it on high and it goes back down. You know, it's like-

[00:34:52] Michael Chan: Yeah.

[00:34:53] Jared Palmer: It doesn't mean that that's efficient.

[00:34:54] Michael Chan: Absolutely.

[00:34:57] Jared Palmer: It just means that it's like, it's gonna get- it's like you're sort of like, that's the mean.

[00:35:01] Michael Chan: Yeah. Yeah.

[00:35:01] Jared Palmer: Right. So there's that. So I I I see what you're saying though. So like React's about like, you describe the business logic and it takes care of the rest. And that's really interesting and and phenomenal, but it doesn't mean it's not without its flaws. Like it doesn't mean that things can't get complicated and like things are complicated with React. Like animations are very challenging.

[00:35:15] Michael Chan: Yeah.

[00:35:16] Jared Palmer: I would say like that is probably like one of the hardest part, like problems that I think is still basically unsolved, but maybe now that we've moved past some other things, we'll tackle that. But yeah, I mean, it's it's gonna be interesting. Like I I I think this will all like be very, very good for the community as as this new like age of Suspense innovation happens.

[00:35:35] Michael Chan: Yeah.

[00:35:35] Jared Palmer: I think I think it will be good, but yeah. So you can store all kinds of things in your cache and it will ultimately lead to just better user experience. And that's what I think the ultimate- like that's what everybody's here for, like just delivering better user experience. Uh and what's interesting about Suspense is that it also kind of provides a better developer experience. And like very rarely do we get to like make that kind of like Like oftentimes you hear like, oh, this is better for developers, but isn't great for the users. And that doesn't make you- those aren't necessarily good decisions. Sometimes we're forced to make those, but sometimes not so much. But this is one of those situations where it's unique in the sense that it's actually good for the users and good for the developers.

[00:36:13] Michael Chan: Well, it sounds like they're making the right thing easy because it's very hard to do all of the the max duration stuff that you're talking about. We're just not gonna do it. It's easier to think like, oh, I have a loading state and a data state. And totally forget about that third state that's really hard to do and like kind of hard to reproduce a lot of the times and has so many like edge cases around it. And it sounds like with this new Suspense component, they've basically made it really easy to opt into a good user experience and consider that third point.

[00:36:43] Jared Palmer: Yeah. I think that, and I think that's like the, that's the, that's the zone, right? So like I'm always about solving for the 80% use case, like make the 80% easy. And then like 20% of problems are like going to be unique no matter what, like you're not gonna be able to cover all your bases. And I feel like that's the case here. Like no, Suspense is not this like silver bullet. You still need to go through all all the motions of like, you need to split out your resources. You need to like lazily load everything.

[00:37:05] Michael Chan: Yeah.

[00:37:06] Jared Palmer: But you don't get this conflict where you have to make a choice between like delivering great user experience for users on slow connections or janky user experience on users for fast connections. Like that's what I, that's what we do. Like you just throw a loader up there. But you know, now we don't have to do that. And that's just a really phenomenal thing. And it seems so small, but I think I'm gonna delete more code in my talk than I'm actually gonna write.

[00:37:28] Michael Chan: Awesome.

[00:37:28] Jared Palmer: So like, that's also should be like the best part. And like, I think also like, don't- I think those are messages like I would convey is like, don't rewrite your app at all. Like all of this stuff is completely incrementally adoptable and and and optional. So like, that's really important. And like, obviously it's important to Facebook to build things that are incrementally adoptable. I think they have like 80,000 React components or something like that. So like, And I think it's like the entire React team is responsible for all of them, like for updating it.

[00:37:54] Michael Chan: So

[00:37:54] Jared Palmer: like,

[00:37:55] Michael Chan: so what are the components uh related to Suspense? Obviously there's this Suspense component. Um, and then there's the concurrent mode component. Is that right?

[00:38:03] Jared Palmer: So yeah. So basically you need to make sure that your app passes strict mode. So if you haven't done this at all, there's a React.strictMode and you wrap your app. You can do this today. React.strictMode. And then fix- click around your app, run through your integration tests if you have them, and fix all the warnings. And it will tell you what is no longer going to be allowed in concurrent mode. Now, you may not be able to fix all of these warnings because you may be using some sort of third-party library or component that is just not gonna do that. If this is the case, you cannot get into concurrent React or take advantage of it all on that slice of the tree. Sure. So what you can do is there's two ways to get to concurrent React, which enables this suspend to work the way it's intended. One way is to replace your ReactDOM.render with this new createRoot.

[00:38:54] Michael Chan: Okay. So ReactDOM.createRoot.

[00:38:57] Jared Palmer: You give it the DOM node and then you call- it returns something which you can call render on.

[00:39:02] Michael Chan: Okay.

[00:39:02] Jared Palmer: Okay. It's a chain thing. That's for when your entire app passes strict mode, you can use that. But you may not be able to get there from day one and that's okay. What you can do is you can import, if there are certain parts of your tree that that do pass strict mode and you can identify them, you can wrap that part of your tree with React.concurrent mode and that part of it will use concurrent mode. So yeah, so that's cool. So basically you can incrementally adopt this concurrent mode and these are just like, these aren't like massive structural changes. They've been warned about them for like at least 6 months. Sure. This is happening. And these are like-

[00:39:35] Michael Chan: gonna be a lot of library forking going on.

[00:39:37] Jared Palmer: Oh, sure. I'm sure. I'm sure. Like I was saying, like refs, right? Like that kind of thing. So like string refs, when those got deprecated, it was like, I think I'm, I think I'm still using my version of like React Modal 2 or something. I don't know. Anyways, you're gonna go through your app and you're gonna take off all the stuff that doesn't work with strict mode. You're not gonna touch it. And then you're gonna wrap the stuff that does work with strict mode in concurrent mode, and then it will enable concurrent rendering and you'll be able to use Suspense. But what's really cool, and this is amazing, is that you can actually still use the Suspense component outside of concurrent mode.

[00:40:07] Michael Chan: Tell me more about that.

[00:40:08] Jared Palmer: Okay. So the Suspense component, we talked about how it pauses, it tells React to wait until resources are available. That only happens if you're running in concurrent mode. So either with createRoot or underneath a concurrent mode component. But if you're not in concurrent mode, you can still like use that component around And it will work. What it will do though is it will immediately fire the fallback that you passed to it. And if you think about that, back to the first thing we talked about, that's actually where you are today.

[00:40:39] Michael Chan: Yeah, yeah, you're not losing anything.

[00:40:41] Jared Palmer: No, it's actually better because you've deleted the loading state.

[00:40:44] Michael Chan: That's awesome.

[00:40:45] Jared Palmer: It's slightly better. It's not- it's not perfect.

[00:40:48] Michael Chan: Sure, sure.

[00:40:48] Jared Palmer: But it is better. You've deleted a ton of state and also like all the interfaces and stuff that goes around with it, right? Like you've deleted is loading. From your application. It just fires immediately at all times. So you're gonna get that jank still, and it's- but it's literally the same jank you already have.

[00:41:01] Michael Chan: So by upgrading, you get to opt into the better developer experience and then kind of slowly prepare your app for the better user experience.

[00:41:10] Jared Palmer: Pretty much. That's a great way of putting it. That's awesome. Um, yeah. So it's, it's great. And so you can start using that pretty much like tomorrow and, uh, playing around with it. And I'm sure people are gonna invent some crazy stuff that we haven't thought of yet. That's really the power of React. It's always gonna be this incrementally adoptable, incrementally improving thing.

[00:41:27] Michael Chan: Yeah.

[00:41:28] Jared Palmer: And like, I hope other updates come out that are like this and like less like the getDerivedStateFromProps thing that like broke for me. Like, that was like not good. It's like, that was, that was, that was a major version for me.

[00:41:45] Michael Chan: Right. Sure, sure. Well, because it throws the warnings, right? And then everyone's pissed at you because you're throwing warnings in their apps, or-

[00:41:51] Jared Palmer: Right, so the getDerivedStateFromProps error, if we want to just touch on that for a second, was like a fundamental change about the way that was supposed to work. So like, basically there was a slight change in the API that like completely changed the way it actually behaved. And like, it was, it was behaving like componentDidUpdate, and then it was behaving like basically like setState during render. So it was doing like setState during render, which, as you can imagine, if you just thought about that, like that's not a good idea. But that's how that's basically what it is now. But that shift was not good for Formic. But I digress.

[00:42:25] Michael Chan: So for all the people who are uh looking into updating their apps to Suspense, um what what's some parting wisdom that you have for us?

[00:42:33] Jared Palmer: You want to start your movement, I'll say, I won't say migration, I'll say movement, basically focusing on 3 things. The first thing you're gonna want to do is load use like react.lazy, which is a helper to lazy load modules. Uh, so if you use create-react-app, you use webpack, if you use Parcel, um, they support dynamic imports. And you can use react.lazy to, in addition uh to Suspense, to more ergonomically uh lazy load your modules. You could do this before with a React component, but it would not be as fun and also as terse. Um, so that that's like thing one. So like module code splitting, that's thing one. Thing two are like assets. Uh, you can use React cache today, tomorrow, to intelligently create placeholders and fallbacks and orchestrate the loading and caching of your app's images, audio, video, script even. Um, and that's really, really awesome too. Uh, and then the last thing that you may need to wait on, and this is my suggestion, is uh data. So you can use React Cache and Suspense to fetch JSON and store it. But there are some fundamental things that are missing right now from React Cache, like normalization. Invalidation, which like pretty much prevent it from being used for that for the moment. But that's okay, that stuff's coming. So just wait. And if you use like Apollo and Relay, like that stuff's coming anyway. So you won't even maybe need to touch it. And if you're feeling adventurous, you should read the docs and read through the source code of React Cache and like get involved.

[00:44:20] Michael Chan: Because it's not that big, right? It's like 400 or 600 lines or something.

[00:44:23] Jared Palmer: Yeah, it's tiny. So uh that's the your 1, 2, 3 steps to moving to Suspense. And again, don't rewrite your whole app. Um, everything's incremental. Uh, remember that. And then what we do is like, we will only rewrite or refactor when we have to update that part of the application again. So we don't tend to our garden just for the sake of gardening. That's the way we like to think about it, right?

[00:44:48] Michael Chan: That's a good analogy.

[00:44:49] Jared Palmer: Right. We don't just like- yeah, we don't just go back and redo things for the sake of doing it. Like, make a plan, Budget a little extra time and then refactor when you get there. Don't just refactor the whole thing or rewrite your whole thing. That's very, very expensive and silly. And you may decide that, you know, we have this really great data fetching technique internally that we invented here and it's really great and it's working for this entire slice of our application. And that's totally 100% okay. Because guess what? It's worked so far.

[00:45:17] Michael Chan: It's making money.

[00:45:19] Jared Palmer: It's making money today. So if it's making money today, you may not want to touch it because Move on to other things that can make you more money. I just like, you know, just don't, don't, don't, don't rewrite things for the sake of it.

[00:45:31] Michael Chan: Don't jump on the hype train.

[00:45:32] Jared Palmer: No.

[00:45:33] Michael Chan: Take it easy. Play with it. Stick to your goals. Know about it.

[00:45:36] Jared Palmer: Know about it. Play with it. Wait, I would wait for like the next revolution to happen and then like pick your winner. Like think about how painful it was when you picked the wrong Flux framework.

[00:45:46] Michael Chan: Or CSS in JS, like any of these things. Just had a proliferation of libraries,

[00:45:53] Jared Palmer: right?

[00:45:53] Michael Chan: And then you were stuck.

[00:45:54] Jared Palmer: Maybe continue what you've been doing for the past like 3 years and just do it like for another 2 months and then make a decision and then go all in on it, or experiment and do your own thing, whatever. What's great about it is all incremental. You can delete it as easily as you can write it. So that's the best part.

[00:46:11] Michael Chan: Well, you know, it's gonna take a while for us to get a stable release of Hooks too, so Maybe that's your maybe that's your goal.

[00:46:18] Jared Palmer: Maybe that's there. So, but any uh aspiring open source uh people out there who want to go make a name for themselves, now is a great time to be in the React community. Thanks. Thank you for having me. This was awesome. Always good to chat. It's been a minute.

[00:46:31] Michael Chan: Um, I I know you got a speaker dinner to attend to, so I really appreciate all of the time that you gave us. Thanks for that very pragmatic message at the end. I'm excited for your talk. We'll obviously link that. Yeah, thanks again.

[00:46:43] Michael Chan: This has been episode 29 of React Podcast. For links to the projects, talks, and everything else mentioned in this show, visit reactpodcast.com/28. As I mentioned in the top of the show, if you are new to React, I have a fun little project for you. I'm going to be doing the second season of React For 25 days in December, I'm going to post one lesson a day on React. These are 2 to 3-minute reads and all of the code will be included. My hope is that you'll go into 2018, whether a beginner or old hat, with a newfound understanding of what React future means for you. Join in on the fun at react.holiday. Thanks for listening. We'll be in your ears again next Tuesday. week.