31: Hooks are Mixins with Ryan Florence
Ryan Florence is the co-creator of React Router and creator of accessibility-first React libraries Reach Router and Reach UI. Chantastic sits with him to talk about Hooks on the night before they're announced. They talk about React's API growth, if Suspense has taken React to framework-land, what caches and resources mean for developers, and the rebirth of mixins as Hooks.
Featuring
React Holiday
Season 2 started last week.
Sign up here!
Links
Transcript
[00:00:00] Michael Chan: This is React Podcast. I'm Jantastic. 'Twas the night before React Conf, and Ryan Florence and I got to sit down and talk about hooks. If you've ever wondered what goes through a speaker's head the night before they show off a brand new feature in a beloved framework, you're gonna love this show. Ryan and I, bordering a state of delirium, just talk about all the things that have been changing in React this last year. We talk about the place that hooks and suspense have in React and the sordid history that brought them into being. I think you'll really enjoy this chat. There are a couple moments that are just straight up goofy. And we'll dive in right after a word from our sponsor. This week we have a silent sponsor. Instead of telling you about their product, they wanted me to tell you about Gabe, Gabe Greenberg. Gabe has long been the community leader of React. ReactiFlux, a Slack-based community for React and JavaScript developers. And for a really long time, Gabe has had a debilitating illness that his insurance does not cover. He's a great guy and friend of React. He has a GoFundMe page set up at gofundme.com/help-gabe-get-healthy1. You can also find the link placed conveniently in our show notes at reactpodcast.com/31. Take a moment to go to the page, read about his story, and if nothing else, else, send him a kind message thanking him for his community support. So this morning you tweeted, and I quote, "React is getting too big." And you respond, "React hasn't changed in 5 years. Code using the new APIs is simpler. React is generally backwards compatible." Sound familiar? It's the same story as JavaScript and it seems to be working well there. Only other option is do nothing now. Thumbs down.
[00:02:09] Ryan Florence: Thumbs down emoji.
[00:02:10] Michael Chan: So what inspired that?
[00:02:12] Ryan Florence: I saw a couple of tweets where people were like, so, so React just released, what is it? Memo?
[00:02:18] Michael Chan: Memo. Yeah.
[00:02:19] Ryan Florence: React Memo and React Lazy. And then people started tweeting about like, oh, and then you add in Suspense and the React API is just getting huge and they're just throwing more and more crap at it. Um, and uh it frustrates me because like, and it's a little bit unfair for me to tweet that today because I'm thinking mostly about Hooks.
[00:02:44] Michael Chan: Yeah.
[00:02:44] Ryan Florence: Which no one has- well, a few people have seen. The people who are speaking at this conference and a handful of other people have seen it. But yeah, uh the just because API is gonna get way smaller. I mean, there's gonna be more API. But how you actually- in the future, your React components, the API is gonna be so much smaller. The the APIs that you actually use to get the job done um is is gonna feel so much smaller than what it is today. And so it's like, I just, I don't know. I'm trying to trying to preempt and hold back the wave of like negativity about new stuff in React.
[00:03:23] Michael Chan: Yeah.
[00:03:24] Ryan Florence: Because I just don't want people to be like, oh no, it's it's Angular 2 all over again, and it's it used to be such a simple library and now it's a framework. And it's like, it was never just a library, it was always kind of a framework, and it still is going to be kind of a framework, but it's it's so much simpler.
[00:03:42] Michael Chan: A lot of people talk about that line between library and framework. What does what what does that mean for you, or like, what do you think they're talking about? Obviously everyone has different definitions about where that line is, so it's kind of unclear to talk about.
[00:03:55] Ryan Florence: There's that thing that everybody says, you call the library, the framework calls you. So you hand code to Rails, you say here's my controller, and then Rails calls all the methods on it. You hand Ember your whatever. I haven't used Ember for too long.
[00:04:14] Michael Chan: It's been a minute.
[00:04:17] Ryan Florence: Yeah, and then Ember will call the model hook on your route. Or whatever. And uh it's funny that people have called React a library on that definition, because you hand your top-level component to React DOM dot render, and then it calls all the lifecycle hooks on it.
[00:04:33] Michael Chan: Yes.
[00:04:35] Ryan Florence: So like, I guess it's a framework.
[00:04:39] Michael Chan: Yeah,
[00:04:39] Ryan Florence: it always has been.
[00:04:40] Michael Chan: That was one thing that kind of caught me off guard about hooks when Dan showed it to me. My first instinct, the thing that kicked up in me, the feeling was oh, it feels like I'm losing control without these classes and instances and the lifecycle methods, right?
[00:04:56] Ryan Florence: Oh, really?
[00:04:56] Michael Chan: Because I'm handing it- like, I'm handing that over to React. And you actually wrote a post with the title like, React Take the Wheel, which I thought was funny, by the way. Um, and that's true. I feel like it's- it does feel like that, where I'm like handing that over. But he kind of reminded me it's- it's almost the inverse, because you don't have control over when those methods get called in the components or component classes.
[00:05:21] Ryan Florence: Yeah.
[00:05:22] Michael Chan: Um, they're getting called regardless. And like, you don't have control like whether or not they're called. If you're using them or not, they're still gonna get called. And that was an interesting shift for me to realize that like I am kind of delegating like a level of control over these things, but I'm also gaining control of doing only exactly the work that I needed done.
[00:05:42] Ryan Florence: Yeah. Yeah. When I was when first started using React, it reminds me of— uh because we were coming from two-way data binding. And then uh with React, you got to say setState. Like, you get to say when we're going to update. And now with uh with like useEffect, you get to say, okay, right here, I want you to now do this thing. Instead of- well, the big the big change that I've been thinking about and like the phrase in my head is um Uh, the quote unquote old API of React, we need a term for that, and it shouldn't be legacy. Um, but before hooks and before Suspense, and before the new lifecycle hooks in classes, uh, React told us what it was doing. Component will mount, hey, I'm about to mount this. Component did update, I updated this. React is just telling us what it's doing,
[00:06:38] Michael Chan: and
[00:06:39] Ryan Florence: then it was up to us to figure out what the heck we're supposed to do about it. And so, if we needed to fetch data, uh we need to do that in didMount and in didUpdate. But componentDidMount and componentDidUpdate, those don't say anything about reading data.
[00:06:55] Michael Chan: Yeah.
[00:06:55] Ryan Florence: Or resources or reading data or anything like that. It's just like, I mounted, so you can go do whatever you want to do. But now with Suspense, you get to say createResource. This is- or with React Cache. Create a resource. This is I'm defining a resource for how to get data. Instead of just like do whatever you want in componentDidMount, there's a thing called a resource now. It's still as simple. Uh it's even simpler. You actually just return a promise instead of call a promise and then setState. You just return a promise. So the so the code you actually write, the API got a little bit smaller to make a resource, even though we have a new API. Um, and then when you want to read that value, you do resource.read. Right, so now now it's a very direct thing. We're not just happening to fetch data in componentDidMount. React tells us what it's doing, then we decide what to do about it. Uh, we actually have a very direct API to say, here's a resource definition, and now I want to read from it. And then uh you think about like get snapshot before update. We used to do that stuff in uh componentDidUpdate. And we'd take a DOM measurement, see what's focused, see what the scroll position is, put it on an instance like this.scroll, scroll position, or scroll at bottom or something, right? You're in a chat app, and you want to keep the scroll position at the bottom, so you're gonna check right before we update, uh where's the scroll position, so that in componentDidMount, you can then move the scroll down.
[00:08:22] Michael Chan: Yep.
[00:08:23] Ryan Florence: Now we have getSnapshotBeforeUpdate, which tells us what we need to do. So React isn't saying, hey, I'm about to update, hey, I just updated. It's saying, let's get a snapshot before the update. So now we're talking directly about what we need to do.
[00:08:39] Michael Chan: Yeah.
[00:08:39] Ryan Florence: Get derived state from props instead of component will receive props. It's like, okay, what does what does that mean? What do I do about that? And that hook is the biggest disaster because everybody was doing all sorts of stuff when all we were ever supposed to do with it was call setState. Man, I'm on a soapbox right now. Um, but all we were ever supposed to do was call setState inside of component will receive props. But we were fetching data, we were moving scroll positions, like we were doing all sorts of stuff in there. But now React has a hook that says get snapshot before update. Or sorry, uh get derived state from props.
[00:09:15] Michael Chan: Yep.
[00:09:15] Ryan Florence: Like this is what we need out of this hook. So instead of this kind of big unwieldy set of hooks that we're not quite sure what to do in each one of them, yeah, we have a couple more pieces of API, but the code is actually simpler because We know exactly what we're doing. And then I guess finally, with hooks, um, instead of code spread out throughout a class, we got the state, we got componentDidMount, we got componentDidUpdate, we're gonna perform these side effects across 3 different lifecycle hooks. Uh, now it's all in just one effect.
[00:09:46] Michael Chan: Yeah.
[00:09:47] Ryan Florence: So instead of React saying, hey, I'm doing all these things, and then we have to piece it all together in the class, we get to just write it in 3 or 4 lines of code inside of an effect, and we get to say, hey, React, use this effect.
[00:10:00] Michael Chan: Yeah.
[00:10:01] Ryan Florence: Instead of React saying, hey, I just mounted, what do you want to do about it?
[00:10:04] Michael Chan: Yes.
[00:10:04] Ryan Florence: I just updated, what do you want to do about it? We say, React, use this effect.
[00:10:09] Michael Chan: Yes.
[00:10:10] Ryan Florence: So yeah, all all of these APIs to me are just making everything more direct and uh instead of loosey-goosey.
[00:10:18] Michael Chan: That has always felt very weird, the componentDidMount and componentWillUnmount. And the fact that you have to do- you have to separate your work in those two places.
[00:10:30] Ryan Florence: Yeah,
[00:10:31] Michael Chan: it's like do the work, do the cleanup. And it it has always felt a little bit divorced from kind of the intent, I guess.
[00:10:38] Ryan Florence: And then store all the junk on a class, which is like a terrible- yeah, they're so hard to work with.
[00:10:45] Michael Chan: Yeah. Okay, so let's back up a little bit and let's talk about hooks in general. What are hooks?
[00:10:51] Ryan Florence: Hooks are all of the uh all the stuff we used to do in lifecycle hooks, but better. Uh in in Dan's talk, do we know what night it is when we're listening to this podcast?
[00:11:06] Michael Chan: I always do them at exactly the time that we're doing them. So
[00:11:09] Ryan Florence: okay, so Dan's going to say in his talk tomorrow, right at the end, uh he did a dry run with me. Um, and it was it was really good. I'm excited for it. But anyway, he will say tomorrow, and did say to the people listening to this podcast, um, he said at the end of his talk- we spent way too much time on on this, uh, he said it then, or will say at the end of his talk. This is so confusing, like my mind is exploding.
[00:11:38] Michael Chan: I'm gonna ask the question again.
[00:11:41] Ryan Florence: No, okay, I got it.
[00:11:42] Michael Chan: Just do it right as as a No, because I have no idea how I'm gonna edit that.
[00:11:47] Ryan Florence: Don't edit it, just leave it in there. It's gonna be great. Okay, so, um, uh, Dan says in his talk- there we go, we'll do it like the literature version. So Dan says in his talk-
[00:12:00] Michael Chan: It's an immutable state. It has happened and will happen.
[00:12:04] Ryan Florence: Yeah, and it's happening now. Uh, I don't even know what we're talking about anymore. Oh yeah, he says at the end of his talk- okay, I remember what we're talking about- that um that the word atom, uh atom means indivisible. So when when scientists discovered the atom, they were like, this is it, this is the smallest unit of the world, of the universe, are atoms. So that thing is indivisible, it's the last, it's the last particle. And then later they discovered electrons, and they were like, oh.
[00:12:42] Michael Chan: Naming is hard.
[00:12:43] Ryan Florence: I guess there are smaller parts. And um in in React, it's sort of like components, at least the way I kind of interpreted uh what- maybe this isn't exactly what Dan meant, uh but it's kind of how I interpreted it. Uh because I've always thought of components are the thing, components are what React are is. Um, but now we've discovered, wait, no, there's there's a thing that was always there. We were doing it in the lifecycle hooks spread out across a class, but there was a thing that was there, we just hadn't discovered it yet.
[00:13:13] Michael Chan: Yeah.
[00:13:14] Ryan Florence: And it's these hooks.
[00:13:15] Michael Chan: Yep.
[00:13:16] Ryan Florence: It's it's a way to take everything that we're doing across a class, keeping our state. So we've got useState, um, we've got reducers from Redux, now we've got useReducer. Uh, we had context. Which has been all over the map on how to access context. Yep. And now uh it's just a hook. useContext, pass it the context, and you get a return value. Uh render props. We don't have to deal with render props to get stateful values out of components. Um we can just return them out of a hook. Um if we want to do effects and componentDidMount, componentDidUpdate, all that kind of stuff, uh hooks let us do just anything stateful, anything effectful, if that's a word. Are there. So it's it's something that has always been there in React. It was just spread out across classes and we hadn't really discovered it. And now it's like electrons. We discovered the electron and we've given it a name. This is a hook. And so now we've got a way better way to compose uh all the stuff we were already doing before. It's all the same concepts. It was all there. Um it's just uh some more direct API for it now.
[00:14:21] Michael Chan: Yeah, so these hooks, are they exclusive to function classes? Or sorry, function classes, function components?
[00:14:28] Ryan Florence: Sure. It is late, isn't it?
[00:14:32] Michael Chan: Note to self, don't record at 11:30. Um, are these exclusive to function components, or are they able to be used in class components too?
[00:14:43] Ryan Florence: Just function components.
[00:14:44] Michael Chan: Just function components.
[00:14:45] Ryan Florence: But the same codebase can have class components and function components. So, um, you don't have to migrate everything over. Uh, you can just do new stuff with hooks if you like them.
[00:14:55] Michael Chan: Yeah. So tell me, um, which hooks are there? What are the uh the the hooks we have to work with?
[00:15:00] Ryan Florence: Uh, I'd have to pull up the docs. But um, the ones that you'll use all the time are uh useState, and that replaces setState and this.state. And uh useEffect, and that replaces, I mean, all of the lifecycle hooks that we used to use. But but generally, you can simply think about it as just didMount and didUpdate. useEffect is anytime we render- so React knows how to render DOM elements, right? And that's that's uh that's that's all those elements are about. And so useEffect is saying, hey, if if you want to take the state of this app, and instead of passing it into element attributes, you can do some other kind of effect with it. Yeah, so it's it's just like didMount and didUpdate. Um and then there is uh useMemo. And that lets you memoize a value. Um, there's useReducer, so you can manage all of your state in a reducer with dispatching, which is really nice. Um, useLayoutEffect and useMutationEffect.
[00:16:04] Michael Chan: And those are just specializations of useEffect, right?
[00:16:08] Ryan Florence: I don't know what they are yet. The React team knows that there are some weird edge cases.
[00:16:13] Michael Chan: Yeah.
[00:16:13] Ryan Florence: And so, we need a couple fancy ones, but probably fine without them is my guess.
[00:16:20] Michael Chan: That is one thing that's that has been a little bit confusing um up to this point, and I'm sure that that it'll get clarified and we'll start to understand it uh in the same way that everything's confusing that it's new when it's new.
[00:16:31] Ryan Florence: Yeah.
[00:16:32] Michael Chan: Is um the introduction of the scheduler. Like it feels present in a lot of these APIs. I don't know. My understanding is that the difference between useEffect useMutationEffect and useLayoutEffect really comes down to how they're scheduled and prioritized.
[00:16:48] Ryan Florence: Yeah.
[00:16:48] Michael Chan: And so there's this thing that's kind of almost like the elephant in the room, I guess. And I'm not sure if like that's what makes it feel like a framework, that there's this thing that is kind of invisible but present and like hovering in the space between these APIs that we're not quite sure about yet. Um, but that's a that's an area that I've felt kind of uncomfortable, I guess, in my knowledge.
[00:17:11] Ryan Florence: Yeah, um, my understanding is you'll need like useLayoutEffect when um you're measuring a DOM element. So React actually does- I I don't know all the details of it anymore, but I used to know them better because I was working on something where it mattered, but it's been a couple of years. But React, in componentDidMount, if you measure an element and then setState like right then, it actually it actually kind of like catches that and then only does one full render or something.
[00:17:41] Michael Chan: Oh, OK.
[00:17:42] Ryan Florence: I can't remember. But you it has something to do with that kind of stuff. But um yeah, you probably won't won't need it. Because most of the time in useEffect, you're rendering to the document title or you're I don't know managing focus.
[00:17:57] Michael Chan: Subscription.
[00:17:58] Ryan Florence: Yeah, you're doing a subscription to something. Uh oh, and then there's useRef. So if you need a ref for an element, you just say useRef and then drop it on the element.
[00:18:06] Michael Chan: Nice. Yeah, that's one thing that I've really liked about the about useEffect, which feels like the workhorse to me.
[00:18:12] Ryan Florence: Yeah, that's my jam. I'm just like, my code is all useEffect now.
[00:18:17] Michael Chan: It should be part of your- like if you had like Ryan Florence and the useEffect.
[00:18:21] Ryan Florence: It's a great band.
[00:18:24] Michael Chan: Yeah, I feel like you get like a lot of like band names through these conferences. What was it? What was DKPiano's name for you?
[00:18:31] Ryan Florence: It was Ryan Florence and the Set State Machine. I've actually never even heard, that I know of, or at least didn't recognize a Florence and the Machine song. Really? Yeah.
[00:18:46] Michael Chan: Wow. I
[00:18:47] Ryan Florence: mean, I'm sure I've heard it, but like, it's not, it's not a group I've ever paid attention to.
[00:18:51] Michael Chan: Yeah, same. I, I just know that I- it, it like catches my ear a little bit wrong most of the time.
[00:18:58] Ryan Florence: Yeah.
[00:18:59] Michael Chan: So kind of back to the component thing. One thing that I have have loved is that whereas in the class component model, subscribing and unsubscribing or doing some type of mutation and cleaning up after yourself have have been separated by lifecycle events, um now with with hooks and useEffect, those are placed right in the same in the same function.
[00:19:22] Ryan Florence: Yeah.
[00:19:23] Michael Chan: Which is really neat.
[00:19:25] Ryan Florence: Yeah.
[00:19:26] Michael Chan: I think.
[00:19:27] Ryan Florence: Yeah, well, because uh what I love about it is that you can use just local variables inside of the function. So you don't have to do like this.interval equals setInterval.
[00:19:37] Michael Chan: Yeah.
[00:19:37] Ryan Florence: And then componentDidMount or willUnmount, um clearInterval this.interval. You don't have to put all that junk on the instance and track it all over. It's just let interval equals setInterval.
[00:19:47] Michael Chan: Yeah.
[00:19:48] Ryan Florence: Return a function to clean it up. Clear, clear interval, interval. I'm saying interval way too much. But yeah.
[00:19:54] Michael Chan: It kind of reminds me of like mixins, right? Where you could like-
[00:19:57] Ryan Florence: They're totally mixins. They are mixins. Yeah, but done right.
[00:20:01] Michael Chan: Which is awesome.
[00:20:02] Ryan Florence: Yeah,
[00:20:03] Michael Chan: because there were some cases where mixins were awesome, especially if you wanted to share- if you wanted to share those lifecycle methods between components, that was a great way to do it. I mean, like, you know-
[00:20:14] Ryan Florence: That's a better answer to the earlier question. What are hooks? Hooks are mixins that don't suck.
[00:20:21] Michael Chan: For function components.
[00:20:22] Ryan Florence: Yeah. Yeah. Hooks are mixins.
[00:20:26] Michael Chan: That actually helps. I mean, now that we've landed on that, that helps me think about them a little bit easier.
[00:20:32] Ryan Florence: Dan tweeted like in March, the future of React is mixins. And then uh when they when they approached me about about this talk and showed me hooks, I was like, oh, that's what that tweet was. He's like, yep.
[00:20:52] Michael Chan: I feel like, I feel like the React Core team is always seeding the future in their tweets.
[00:20:57] Ryan Florence: I know, like, and like, they'll get into arguments with people about stuff. Like, not, not arguments, but they'll say something, people are like, well, what do you mean? And then they're just like, oh, well, I don't know. But, and then 6 months later, you're like, oh, that's what, that's what they were talking about.
[00:21:12] Michael Chan: Yeah.
[00:21:13] Ryan Florence: But like, it just seems like nonsense to us.
[00:21:15] Michael Chan: Those are- we talk about religious things quite often, but like, it's like those verses in the Bible where Jesus is like, oh yeah, that's right, you think this, but in reality this is the case. Yeah. I feel like that's the, the way that the React team talks about it. Oh yeah, you're thinking about React today, not React tomorrow.
[00:21:36] Ryan Florence: Yeah, I just imagine Sebastian dressed up like Jesus. He's got a calming effect on me.
[00:21:45] Michael Chan: He's- oh, he's very calming. You know, Sebastien comes up in this podcast a lot and no one knows how to pronounce his name correctly.
[00:21:55] Ryan Florence: He's actually given up and he just says Marc Beige like everybody else now.
[00:21:58] Michael Chan: Yeah.
[00:22:00] Ryan Florence: But it's like-
[00:22:01] Michael Chan: Everyone is always like, oh yeah, you know, that talk by Sebastien Marc Beige. I'm saying his name wrong and I have to edit it out every time.
[00:22:11] Ryan Florence: It's it's like uh Mark Boga.
[00:22:13] Michael Chan: Yeah.
[00:22:14] Ryan Florence: Is what it really is.
[00:22:15] Michael Chan: It's sounds that we do not have in the English language.
[00:22:18] Ryan Florence: Yeah. I so before I do any sales calls with anybody, um if they don't have like a uh English name that I know how to pronounce, uh I always go on YouTube and like search search for names. And there's all these videos to how to pronounce it, and I'll sit there and practice it before I'm on my sales call with the person. And uh and then one day I was like, wait, I don't know how to really pronounce Sebastien's last name. So I, I wasn't doing a sales call with him, but I don't know what I would sell him. Do you want me to train you on React? Um, but anyway, I still couldn't quite get it in there too because it sounded like there was a T at the end. It was like Mark Bogut.
[00:22:56] Michael Chan: Yeah,
[00:22:56] Ryan Florence: I'm like, there's not a T.
[00:22:58] Michael Chan: Yeah, anyway. Maybe I need to start sending those to everyone I have a call with. So, so your talk tomorrow morning is um 90% cleaner code?
[00:23:13] Ryan Florence: Uh, 90% cleaner React.
[00:23:16] Michael Chan: 90% cleaner React. Um,
[00:23:18] Ryan Florence: what is- I should make a joke referencing back to your React Rally talk. Michael Chan was wrong. Clean code's not dead.
[00:23:25] Michael Chan: Oh, no, that would just give Donovan way too much fuel to to shame me with. So um so what is it about hooks that allows you to make your code that much cleaner?
[00:23:39] Ryan Florence: Some components get really big, and you do multiple things in the lifecycle hooks. And if you watch Dan's talk, uh he he addresses that, where you know you're changing the document title and you're setting up a subscription to the window resize. You're doing both of those things in didMount. And uh and then in uh willUnmount, you're cleaning up uh one of them. And componentDidUpdate, uh you're working with the document title again. And so you just have— um you'll have a single concern, like managing the subscription to the window size, and it's spread across 3 different lifecycle hooks.
[00:24:16] Michael Chan: Yeah.
[00:24:17] Ryan Florence: And then you'll have another concern, updating the document title, and that's spread out across 2 lifecycle hooks. And the code for the document title and the subscription for the window resize are right next to each other. But they're unrelated, but they have to be by each other because React is telling us what it's doing. I'm mounting, okay, here are all the things we have to do. I'm unmounting, here are all the things we have to do now. And so, you've got these related effects. That's a pretty bad- Unrelated effects, yeah, they're unrelated effects that all live next to each other and just kind of get spread across the class. It just feels so much better when you move all of the document title code into just- it's just 3 lines of code now instead of spread across. Well, and you know what, maybe it's the same number of lines of code too. It's 3 lines, but they're 3 lines that are next to each other. So cleaner code isn't always about less code. Sometimes it's just about if it's in the right box and and where it's co-located. Which we probably don't talk about that enough as a community, how much we value co-location of stuff. Like that's what- That's what got a lot of us into it. JSX is co-located, the styles are co-located. Um, but in a component today, a single concern is not co-located. It's all in one component, but it's spread across all those life cycles.
[00:25:33] Michael Chan: Yep.
[00:25:33] Ryan Florence: So hooks let us co-locate a single concern into just a couple of lines of code. So that's the first part that makes it feel a lot cleaner, is one concern uh you get to manage in just one chunk of code. Like, it's line after line of all the same thing. And then the second thing is that it it is less code. I was just trying to figure out how to actually get a 90% ratio on my refactor.
[00:25:59] Michael Chan: You made the promise in your title, but can you deliver?
[00:26:02] Ryan Florence: Yeah, I don't know. Um, in this little app where I'm looking at media queries and stuff, it went from like, what was it, 64 to like 32 lines of code or something. Um, and everything's there. Like, I didn't lose anything. Um, it's just it's less code, it's more organized, and uh it's really portable too. Like um you watch in Dan's talk, uh he takes um he takes his hooks that are all inline in his app component, and then with just literally cut and paste, uh now it's in a custom hook.
[00:26:36] Michael Chan: Yeah.
[00:26:36] Ryan Florence: And so it's a whole lot easier to to move your code around. To add effects, remove effects, add state, remove state, and then abstract it into something that's reusable.
[00:26:48] Michael Chan: That's the thing that stands out most to me in terms of cleanliness is the transportability of it, right? You can write it inline, like right in your component, and then as you learn that that P that effect or that state or that whatever is extractable, it's so easy to move out and then use other places. And without having to- like, if you were to extract, um, like a subscription or something like that into functions, you'd always have to have two. You'd have to have the, the creation and the cleanup. And now it's just that, that one, which really adds to that portable nature of these things.
[00:27:28] Ryan Florence: And, and when you decide you want to reuse an effect or some state or whatever, All of that code is already right next to each other. So it really is like select those lines of code, cut, paste. Where if you're in a big component across multiple lifecycle hooks, you've gotta find each piece that has to do with that concern and then go paste it into the other spots, into the a new component. So yeah, it's just all all of it is cleaner. There's less code. Uh the code that the a single concern is co-located. And um the portability is is awesome. I don't really address the portability a whole lot in my talk. But yeah, those 3 things I think are what make it just really clean.
[00:28:11] Michael Chan: Yeah.
[00:28:12] Ryan Florence: My uh my React code has gotten incredibly sloppy, like with render props, like they're a disaster. Uh but they're awesome. They're really composable. But that just, you can just stop doing that with hooks and now you don't have this terrible nesting and hierarchy that isn't real.
[00:28:31] Michael Chan: So with hooks, if they're designed to kind of augment function components, there's more being like pushed into render now that we're, I guess, trying to get away from like component lifecycle events. And this seems like to be the story with like Suspense too. Like a lot more is happening inside of render, you know, where you're you're you're getting that data and like whether or not you have it, that component's going to kind of render and or it's gonna, you know, throw and suspend, whatever.
[00:29:03] Ryan Florence: Yeah, I mean, I was kind of, I was kind of clued into that a couple years ago when I redesigned React Router to version 4.
[00:29:13] Michael Chan: Yeah,
[00:29:14] Ryan Florence: I was sitting on an airplane after a workshop, um, and I was like, okay, if I were to redo a router with all the stuff that we teach in our workshops, um instead of the way that we we did it before, and uh and knowing that React actually values stuff in render instead of moving things out of it, what would it look like? And so uh that that was when I first started personally to like push stuff into render. And that's when I started getting really excited about render props as well. Yeah. I gave that talk at uh at React Rally, where I threw a fetch component up on the screen. And uh I've had so many people email me, send me DMs, come up to me at meetups and conferences and just be like, you blew my mind with that fetch component. Um yeah. And it was the first time that a lot of people saw, like, oh, I can I can fetch data right there in render.
[00:30:10] Michael Chan: Yeah.
[00:30:11] Ryan Florence: And then we realized we could do all sorts of stuff. And now the GraphQL uh the Apollo uh API is a render prop like that. Like we already have. So I guess what I'm trying to say is, for the last couple of years, the whole community has kind of already been pushing stuff into render. Uh our only choice for that was render props. And they're— I I just I keep calling them a syntax disaster. Because they are. Oh, man. Walking around in workshops while people are working on render props and like-
[00:30:43] Michael Chan: You forgot a paren. Yeah.
[00:30:46] Ryan Florence: If I don't get it right the first time, I just like undo everything and then like try again. But yeah, so I think we're already going into render. And uh now we've just got more stuff there that— uh but it's there in a good way.
[00:30:59] Michael Chan: Yeah.
[00:31:00] Ryan Florence: Render props, it was there in like a just kind of a hacky way.
[00:31:04] Michael Chan: Yeah, I'm really excited about this because you do have that- there was that problem with the render props. I I wonder if One of the reasons that people are like starting to have concern in kind of posing this idea that React is going more into framework territory is because for a long time when you npm installed React, all you got was a component. And there was something almost comforting about that to be like, oh, as long as I do everything in the component model, like I can do all of these cool things in the component model. So if I want to update the document title, I can do that in a component and then I just compose that in with render props to what was already there. And like all I have is a component. And in a way, that's a liberating constraint, right? I got this one thing that's super freaking awesome and can do anything that I can throw at it. Um, but now it feels like things are being teased out and I wonder if that's where a lot of that fear kind of comes from.
[00:31:57] Ryan Florence: Yeah, I think that's exactly where it's coming from. It's it's just it's one of those things where like on paper it's like, yeah, React was simpler before. Because you only had components. But when you actually like consider everything that you have to do to build a UI, the React API wasn't as big, but everything you had to like deal with to do these, I mean, basic things, update the document title, fetch some data, you've gotta do that in every app. To to get it done, it was bigger. And now with a few more direct APIs, I think it's it's smaller. I I I really gotta come up with words to describe this. Because like, I've got the idea there in my head, but it it can't come out yet in the right words.
[00:32:43] Michael Chan: Yeah, it it it's hard to describe, but it feels more— it it feels more like you're closer to the intent.
[00:32:50] Ryan Florence: It's like when you organize a drawer. It's like, this drawer is a mess, and uh but it's just a big open mess, and all you have is a big drawer, and you can just throw everything in it. And then when you go by the tray to put your your knives and your forks and stuff in, like, there's more API there, but like, it feels way better.
[00:33:10] Michael Chan: Yeah.
[00:33:10] Ryan Florence: You know, it's like, okay, this this is easier to find a fork now. Where before it was like, where the heck is a fork? You're like moving all around to get it. And now, well, the forks are always right here. So I guess I guess I'm saying it's okay to be more like a framework. I mean, I've always believed in frameworks. I've always liked them.
[00:33:28] Michael Chan: Yeah.
[00:33:28] Ryan Florence: Um, it's just when you get it wrong, it's like- maybe that's the fear. People are worried, like, we've uh we've hit the edges of frameworks before where you're like, this framework doesn't know what to do now. I need to do this task, it doesn't know how to do it, and I've got these brick walls all around me in this framework and I can't do something that I need to do. Um, maybe that's the fear that people have, but uh I don't I don't think I don't think that's gonna be how it'll pan out with React. Um and we and you always have the classes. So if you can't get something done with hooks, like we don't have get snapshot before update yet.
[00:34:07] Michael Chan: Yeah.
[00:34:07] Ryan Florence: There there's no hook for that. So you'll need to use a class for it.
[00:34:10] Michael Chan: At least the frameworks that I have seen and the frameworks that I work have worked with, um they feel a little bit more like boilerplate, right? Like, we've looked at all these other applications and these are the general shapes that they take. So if you want to build this type of application, just start from here and you'll have that shape from day one.
[00:34:31] Ryan Florence: Yeah.
[00:34:32] Michael Chan: But this doesn't feel to me like that. It it feels still like we're in the land of primitives where you have a component and you have these hooks that allow you to manage effects And you have these uh these new resources and uh you know Suspense that allow you to manage waiting for data, getting data, how long a user user should wait.
[00:34:58] Ryan Florence: And treating data like they're synchronous values too is is a big deal.
[00:35:02] Michael Chan: Yeah, so so so you have these three things, and I think that immediately it feels kind of confusing, but it doesn't feel like we've gone into framework territory yet because there's really nothing telling you you have to use one or the other.
[00:35:18] Ryan Florence: Yeah.
[00:35:18] Michael Chan: And there's still like a lot of exploration. It doesn't feel like there's boundaries. We're still kind of going, going towards the, the capability. We had- we've had the capability for all this time, but now we're putting things in render. So what's a more suitable way to handle all of those things instead of continuing to have 60 tabs deep, the thing that actually renders out to the DOM?
[00:35:40] Ryan Florence: You try to open up the React DevTools and Good luck. Yeah. Uh, I I just had a thought. It's uh, so before we just had components, but most apps also have Redux. And so it's not just components, we've got everything else that comes with Redux. And if you look at, I guess we'll call it new React, um, you've got components just like you had before. Those render actual elements. You've got hooks. Those uh replace state and life cycles. Those were already there. It's just it's just a a way to use it with function components now. Um and and I think it's a a better way. It's a cleaner way to do it, too. Um so we don't have anything new there yet. And then most people just use Redux as a client-side cache of their server-side data. And so instead of Redux, you have React cache and Suspense. Yeah. And uh if you look at your Redux code to fetch something, like just one thing, right? There's a ton of code for that and several files. But with Suspense and React Cache, you have a resource and a cache.
[00:36:49] Michael Chan: Yeah.
[00:36:49] Ryan Florence: And and when you want to read from that thing, it's literally you just say read and pass it a cache key instead of like all of the boilerplate that goes on with with Redux. So so what I'm what I'm saying, that that original tweet that you read at the beginning of this conversation, that the that the API is actually small, that React is actually gonna feel smaller in the future. That's what I'm talking about. Because it's all this new stuff was already there, just in different forms. Uh but now now we've got uh tighter ways to do it and simpler ways to do it. We get to think about our data synchronously. Um we get to co-locate our effects. And then elements are like they always were.
[00:37:29] Michael Chan: So are you optimistic or pessimistic?
[00:37:32] Ryan Florence: I'm incredibly optimistic.
[00:37:35] Michael Chan: Bullish on React.
[00:37:37] Ryan Florence: There's also a chance I've just been super bored in my workshops after 3 and a half years and I finally have something new to talk about. Yeah, I guess you can't trust anything I just said. When uh when React changes, I'm still in business.
[00:37:55] Michael Chan: Well, I mean, we've talked about it and you're like, and you're like, man, with hooks, I'm I'm uh I might be out of a job in a year because React's gonna be so easy. And I was like, People will find ways to mess it up.
[00:38:05] Ryan Florence: Yeah, I have to battle the next Redux again.
[00:38:08] Michael Chan: Uh, cool.
[00:38:11] Ryan Florence: People are gonna make higher-order effects.
[00:38:13] Michael Chan: Higher-order effects.
[00:38:14] Ryan Florence: Higher-order effects.
[00:38:16] Michael Chan: Oh, of course. We need them. We need them. Where are they?
[00:38:19] Ryan Florence: It's the only way to compose them together.
[00:38:21] Michael Chan: They're missing. Shoot.
[00:38:24] Ryan Florence: Oh, here's here's just a random little tidbit. Uh, if you've used directives before, Like in Angular? Yeah.
[00:38:31] Michael Chan: Oh, dang.
[00:38:32] Ryan Florence: So as I've been screwing around with with hooks, I've realized we now have directives in React.
[00:38:38] Michael Chan: Okay, so directives- I'll say this is for maybe the audience members who don't know what directives are, but I'm asking for a friend. What are directives?
[00:38:48] Ryan Florence: Uh, directives are just attributes that you put on an element and then adds behavior to it.
[00:38:53] Michael Chan: Okay. Yep.
[00:38:54] Ryan Florence: So imagine you could do like div, uh, I don't know, turn red when hovered.
[00:39:02] Michael Chan: Oh, I remember like so in Angular when I did this like I don't know half a decade ago, it was like ng-list or ng-repeat.
[00:39:10] Ryan Florence: Yeah, those ones are like this.
[00:39:12] Michael Chan: It was one and you would do like for name in names.
[00:39:15] Ryan Florence: Yeah, that's kind of a bad example for hooks. But yeah, yeah, you wouldn't be able to do that with hooks, thank heavens. But um, but yeah, ng-repeat, ng-click, all those. But you could make your own custom directives. So you could be like, uh, Michael-turn-red-when-hovered. So you the the user actually got to the developer got to render the div. And then they just said, oh, and I want the behavior that Michael added to turn red when I'm hovered. Uh, in in React today, you would need a whole component that you would probably pass do a render prop with. Or you'd clone your child or something and try to add- you've gotta add like an on mouse enter and on mouse leave of this component. And in React, uh before tomorrow- now I'm confused on the timeline again.
[00:40:03] Michael Chan: It's especially hard because they're all in version 16. Like they've added so much in version 16.
[00:40:08] Ryan Florence: But uh before hooks, uh you wouldn't be able to just randomly add some some uh some events to an element like that. You'd have to ask the user to do it. But now you can have a hook that returns those event handlers and then spread them onto the div.
[00:40:25] Michael Chan: Yeah. OK.
[00:40:26] Ryan Florence: You can abstract just a handful of events that work with some state. Maybe do a side effect. Maybe you want to track when something is clicked or mouse entered for analytics. Um, we kind of have these directives now.
[00:40:39] Michael Chan: Interesting.
[00:40:40] Ryan Florence: Yeah, I think it'll be a pattern that emerges after I used it for a little bit. Uh, I was trying to figure out, well, how do I abstract this? And then it occurred to me, oh, I can spread this stuff in. And then uh I sent a message to Dan, I'm like, hey, I think we're gonna see that we kind of have directives now in React. And he was like, it was just like, yep. And so uh I I realized that they had come to the same conclusion as they've been working with it, that we're gonna see a lot of- or maybe not a lot, but we'll see that pattern happen where you use a hook, it gives you values, and you spread those values onto an element.
[00:41:14] Michael Chan: So it seems like we have kind of two areas for rapid innovation over the next couple years, and that's hooks and caches. Would you say that's the case?
[00:41:29] Ryan Florence: I hope people just use React Cache.
[00:41:32] Michael Chan: Okay.
[00:41:34] Ryan Florence: Yeah.
[00:41:35] Michael Chan: Is that- so is that not a layer that things like GraphQL and other libraries would insert themselves in? Or would they do that at a higher layer?
[00:41:45] Ryan Florence: I mean, I imagine GraphQL is gonna want to- or, you know, like Apollo and Relay and stuff, I imagine they'll probably want their own cache. But yeah, I don't know.
[00:41:56] Michael Chan: So any uh any parting wisdom about hooks? Take it fast or take it slow?
[00:42:01] Ryan Florence: Fast. Yes. I have zero hesitation with them. I am so excited about them.
[00:42:08] Michael Chan: Me too. I've been thrilled by every moment that I've played with them. I particularly love that, you know, despite the fact that more and more people are going to like Flow and TypeScript and transpilation has just become part of what we do, I love that hooks can be shared with zero transpilation.
[00:42:27] Ryan Florence: Yes.
[00:42:28] Michael Chan: They're just functions.
[00:42:29] Ryan Florence: And they're way easier to type because you're not typing a bunch of props. And render props are really hard to type. Oh, so that's another cool thing that makes it cleaner is a lot of stuff that used to be like props going all over the place, they're just arguments to functions now.
[00:42:41] Michael Chan: Yes.
[00:42:42] Ryan Florence: And in fact, function components, props are just an argument to the function, right? So everything's easier to type and uh and it's uh also easier to type. But typing with hooks is probably harder than typing without hooks, like in your hands, like if you're holding hooks in your hands. It's past midnight, we should probably wrap it up.
[00:43:12] Michael Chan: I ventured to the state of delirium. Thanks for having this midnight uh React podcast with me. This is fun.
[00:43:20] Ryan Florence: My pleasure. I just chugged all my water. See ya!
[00:43:29] Promo: This has been React Podcast, episode 31. For links to the talks and projects mentioned in this show, visit reactpodcast.com/31. And to keep up with the workshops and open source that Ryan's doing, visit reach.tech. And finally, I'd like to remind you that this week we started React.Holiday, a fun 25-day advent of React code. We're talking a lot about the same things that we mentioned in this episode, hooks and suspense and what they mean for you in 2019. If you want to get up to speed in a calm and gentle environment, this is the place to do it. Visit react.holiday to subscribe. That's all I have for you this week. Thank you so much for listening. I have a great episode with Vincent Riemer all queued up. So we'll be in your ears again next week.
chantastic.fm