51: Michel Weststrate Wants You To Stop Writing State Reducers
How many times have you written a state reducer?
100 times?
100 times a month?
Truth is, it's tricky for human brains to write performant state mutations in immutable terms.
Maybe you're whip smart and you've got the theory on lock but the resulting "spread hell" is hard to read and edit long term.
Michel Weststrate wants you to stop writing state updates with immutable APIs like spread, concat, and slice and take a second look at mutable APIs like property assignment, forEach, and push.
He's made it really easy
And the React Team finds this idea very interesting.
We talk with Michel about this wild of idea of state producers (not reducers) in Immer, why they're in the spirit of React, his MobX fame, and why — even in 2019 — it's not a good idea to roll your own state management library.
Listen cautiously though.
After this episode, you may never write a state reducer again...
Featuring
Links
- React, transparent reactive programming and mutable data structures | Reactive 2015 Michel's first conference talk, introducing MobX
- MobX — Simple, scalable state management
- Dojo
- React Conf recap: Hooks, Suspense, and Concurrent Rendering — Suspense, Hooks, and ConcurrentMode announcement
- Immer — Create the next immutable state by mutating the current one
- Michel Weststrate - Immer, Immutability and the Wonderful World of Proxies | ReactNext 2018 — Introduction to Immer and "spread hell"
Sponsors
G2i
G2i is a hiring platform for remote developers devoted exclusively to React and React Native. We vet, you get hired. g2i.co
Transcript
[00:00:00] Michael Chan: This is React Podcast. I'm Chantastic. Hey there, React Podcast friends. I'm back from vacation and I've missed you. Today I thought we'd start with a question. How many times have you written a state reducer? 100 times? 100 times this month? There's one big challenge with state reducers as they are today. It's tricky for our human brains to write performant state mutations in immutable terms. Even saying that just kind of gave me a headache. Now, maybe you're whip-smart and you've got the theory on lock, but the resulting spread hell is hard to read long-term. Michele Estrada of MobX fame wants you to stop writing state updates with immutable APIs like spread, concat, and slice, and take a second look at mutable ones like property assignment, forEach, and push. He's made it really easy, and the React team finds this idea very interesting. I sit down to talk with Michele about this wild idea of state producers in Immer, why they're in the spirit of React, and why even in 2019, it's not a good idea to roll your own state management library. Listen cautiously though. After this episode, you may never want to write a reducer again. This week is brought to you by G2i. Some developers are generalists. That's great. Great for them, but if you're listening to this show, you're probably not. You found a perfect hammer in React and you're looking for nails. To find React opportunities, you could join an agency or a consultancy and hope to land on a React project, but you'll likely end up patching WordPress or updating Drupal plugins. Not with G2i. G2i only works on React and React Native projects. That's it. So you get to wield that React hammer like the god of thunder himself. Beyond that, they do their best to match you with projects that showcase your unique skillset. Great with Redux? You got it. Prefer Apollo? No problem. Great with styled-components? Let's do this. Check out G2i today and get matched with React roles that are fully remote at companies who are serious about quality software. Go to g2i.co and click the For Developers link to discover opportunities. G2i, we vet, you hire. It's that simple. Michelle, welcome to the show.
[00:02:45] Michel Weststrate: Hi.
[00:02:47] Michael Chan: It's good to have you. I have um I've been really excited about this call for a long time. I've been like, it's been on the calendar and I've just been like waiting to to to to get to it. How are you doing?
[00:02:58] Michel Weststrate: I'm doing very well.
[00:02:59] Michael Chan: Good.
[00:03:00] Michel Weststrate: Been traveling quite a lot like last week, and now I'm back home again. It's always better if you were away for a while. So, yeah, I'm good.
[00:03:10] Michael Chan: Now, are you traveling mostly for conferences right now or or or work stuff?
[00:03:14] Michel Weststrate: No, mostly conferences.
[00:03:15] Michael Chan: Gotcha. About how many conferences do you do every year?
[00:03:18] Michel Weststrate: Uh, I try to stick to about 7, 8.
[00:03:25] Michael Chan: Okay, nice. And uh mostly mostly in Europe?
[00:03:29] Michel Weststrate: Yeah, yeah, mostly uh Europe. Okay, cool. So 2 weeks ago I was at uh React Next and React Conf. Uh and also did a meetup at uh Wix.
[00:03:42] Michael Chan: Oh, awesome. So how has speaking um been for you and and how long have you been doing it? It's a pretty big transition when you go from kind of just like individual contributor to a company, or at a company, uh to kind of like speaking and sharing your ideas and you know talking to big crowds. How has that how has that affected uh like the way you think about things, um kind of your experience as a developer and working on teams? Like how's your experience been?
[00:04:11] Michel Weststrate: I think that the most interesting thing you learn from it is that like you learn to express your thoughts more clearly. Like, like, you have to um make sure to get a point across in a pretty small amount of time without repeating yourself too much.
[00:04:31] Michael Chan: Yeah.
[00:04:32] Michel Weststrate: And I noticed that, like, using metaphors a lot, really, that's something uh cool I learned from that. But so yeah, it went basically from uh nothing to doing to doing it frequently. It's quite a funny uh quite a funny road. I think I started, my first talk was I think exactly 3 years ago or something.
[00:04:54] Michael Chan: Okay, which conference was that at?
[00:04:56] Michel Weststrate: That was Reactive Conf.
[00:04:58] Michael Chan: Okay.
[00:04:59] Michel Weststrate: The first edition. So uh I'm still not sure why they did ask me. But I'm really glad I did. I don't think my first talk was that great, um, but it was a good start.
[00:05:13] Michael Chan: Was it on MobX, your first talk?
[00:05:15] Michel Weststrate: Yes.
[00:05:16] Michael Chan: Nice.
[00:05:17] Michel Weststrate: Back then it was called actually Observable, but yeah, it was uh it was the same thing.
[00:05:21] Michael Chan: That's awesome. I have noticed in your talks specifically, um, that you really do spend a lot of time trying to get those analogies, um, right, um, which I really appreciate because I am I'm one of those people that's very dense when it comes to new material. Uh, so it's having those parallels has always helped me and be like, oh, okay, like I I have at least like a decent mental model for what this thing does now. Even if I don't fully understand it, like I can uh I I can understand it a little bit enough to like hear why it's why it's good.
[00:05:55] Michel Weststrate: There's a funny uh story behind this. Because the first public uh speaking I did was not like on tech, uh but I occasionally speak at my local congregation.
[00:06:10] Michael Chan: Oh, wow.
[00:06:10] Michel Weststrate: And so there, uh analogies uh uh and metaphors are even more important than like in a tech talk. And so I really noticed that that's uh keeps the attention uh to the talk.
[00:06:26] Michael Chan: Yeah, I absolutely agree.
[00:06:27] Michel Weststrate: Yeah, that was something I brought from uh that area into my talk, which is really helping, I feel.
[00:06:35] Michael Chan: Yeah, I've noticed the same. I think that so much of my speaking is inspired by um by our church and kind of heavy analogy usage. And it's I think it really is a valuable tool, and especially when you have a very short time to get uh get an idea across. Um, it it really helps kind of the taking something that someone already understands and mapping that to something new is such an amazing tool. So how did you um how'd you get your start in programming? Have you been a lifelong programmer or uh has did you kind of transition careers um after a certain period of time?
[00:07:15] Michel Weststrate: No, no, I have been a lifelong programmer. I think I Started, um, maybe even at primary school, but I think a little bit after that.
[00:07:27] Michael Chan: Really?
[00:07:28] Michel Weststrate: Yeah, basically my brother had a, uh, like IBM 286, uh, and he was playing games on it, and I was playing with Visual Basic, I think, or something like that.
[00:07:42] Michael Chan: That's awesome. What was one of the first programs that you ever built?
[00:07:46] Michel Weststrate: That was when I had uh Latin classes. I still don't speak a word Latin. I had classes at one time and then I built a small application like to practice the words. Okay. And eventually I sold that application to my teacher. That's it. So that's the first time I made money uh by programming.
[00:08:09] Michael Chan: That's amazing. Do you remember how much you sold it for?
[00:08:14] Michel Weststrate: I think for something like €100 or something.
[00:08:17] Michael Chan: Wow, that's awesome.
[00:08:19] Michel Weststrate: And so it was to be used by all uh Latin classes, so it got installed on the school network, uh etc. I'm not sure if that many people in the end actually used it. But look at the code either, but-
[00:08:36] Michael Chan: That's incredible. So is it kind of like Like a flashcard type program?
[00:08:41] Michel Weststrate: No, it was a Delphi program. Oh, wow. And um, I think it actually had some support for grammar in there or something.
[00:08:53] Michael Chan: Wow.
[00:08:54] Michel Weststrate: I think it had words and putting uh verbs in the right form or something.
[00:09:02] Michael Chan: That's amazing. I remember when I when I When I first started, I took a class and we would do some of those- my friends and I would make those kind of word games where you would like create your own adventure where it would like ask you a question and then you'd like say like I want to go this direction or that direction and then you would either like get stabbed or like find a pot of gold or something like that.
[00:09:25] Michel Weststrate: Those games are still fun to play.
[00:09:28] Michael Chan: They really are. Yeah, I still- every once in a while when I see one pop up, I'm like, I'm gonna spend at least a half an hour on that. Why not? That's awesome. So what brought you then to React?
[00:09:39] Michel Weststrate: Wow, that's a fast forward of a few years. So at the company where I work, Mendix, we have we have this um application development studio. Um nowadays they're called uh low-code platforms. Um but anyway, the idea is that you have like like you have a Microsoft Access or something, you have a studio where you design applications. And that was that is a uh desktop application written in C#. And at some point, we figured we wanted to have an uh light edition of that, uh web-based. And so at that point, you discover that like all the architecture that does make sense in desktop application doesn't make sense in uh web applications. Uh how you load assets, uh how you update the screen, uh etc. And so most importantly, we noticed that like what we did in the uh C# codebase is just if something changes, we re-render the whole uh UI. And it totally doesn't matter because it like performs like crazy. But when you use the DOM, you totally can't do that because it's like horribly slow to just-
[00:10:49] Michael Chan: Yeah, to reinsert a whole tree.
[00:10:52] Michel Weststrate: Exactly.
[00:10:53] Michael Chan: Yeah.
[00:10:54] Michel Weststrate: And so that was the point like, What framework can help us there? That was basically the question we asked ourselves. And so what we really liked about React was like the neat component-oriented architecture, which makes a lot of sense. Also very relatable how desktop applications are built. But then the problem was there was no way to efficiently update the directory. If your state lives outside your tree. So you have to subscribe and then manage all those subscriptions. And that was eventually the uh problem MobX set out to solve.
[00:11:35] Michael Chan: Mm-hmm.
[00:11:35] Michel Weststrate: But in short, that's how we entered uh React land. So we wanted to uh have a very component-oriented uh architecture where we still had the feeling like we had lots of control. So in Angular, you also I work in a component-based world, um, but the level of control you have feels a bit uh lower, a bit less.
[00:12:02] Michael Chan: So you mentioned working in C#. Was was your experience in React, was that the first JavaScript experience that you had? Or um and then also, how was it kind of coming from C# into JavaScript? Because I know that a lot of C# sharp is a pretty beloved language. It has a lot of like really nice uh language features that um that don't exist in in JavaScript at all.
[00:12:26] Michel Weststrate: Yeah, that's true. Um, no, I did uh JavaScript before. I uh uh I did quite some uh work with Dojo.
[00:12:34] Michael Chan: Okay.
[00:12:35] Michel Weststrate: Um, so so I think the first like really big JavaScript framework, which also has a concept of components, et cetera, but uh well, it's all a lot more verbose and uh dated. Um, but so in that sense, I didn't have too much trouble of switching to uh to JavaScript.
[00:12:57] Michael Chan: Nice. You you'd mentioned uh MobX and trying to solve the problem of an external state tree. Is there something similar in the tooling that you were using in C# sharp that you borrowed those ideas from?
[00:13:10] Michel Weststrate: Um, no, because in uh, yeah, I I think something exists actually in C#, sharp, but we didn't use it. Okay. But we really didn't need to, because like everything was fast enough to natively update everything all the time.
[00:13:25] Michael Chan: I see.
[00:13:26] Michel Weststrate: So that was the that was the big difference uh for us. So our goal was to write components uh as if they are updated automatically all the time. So that's basically the objection that MobX makes. Um, people often consider it more like uh a way to think about reactivity and state management, and that's all true, but the ultimate goal was like, I wanted to write a component. It's it's just a render function, uh, and I don't want to be thinking about when it should update or how it should update or what it should connect to. That is basically the the goal of MobX. So you write the components as if they are rendered automatically all the time whenever uh they should render.
[00:14:14] Michael Chan: Yeah. Well, it's an insanely popular project. I I think that there's very few uh kind of React state management libraries that have kind of ascended to that that state. And definitely you're part of that discussion with, like, you know, Redux, which is like insanely- like had like meteoric success. But it's always like kind of like Redux versus MobX. Um, so, you have like an amazingly successful product. Was this your first project on this scale in open source?
[00:14:48] Michel Weststrate: Uh, yes. I did a few small libraries, but more for internal purposes. But this is the first one I made public.
[00:14:57] Michael Chan: How was that? I mean, that that seems like a pretty big jump going from kind of like just kind of a handful of projects to something that really is in that like zeitgeist of of React. Was it overwhelming to have a project that was in that people many people's minds and apps?
[00:15:13] Michel Weststrate: Yes, definitely. Um, but also, it's it first it took a while. And then at some point you start to discover all the places it is used. Because that's funny about open source, you can write an open source library and you just don't know where it's used. And yeah, at some point people from Microsoft and people from DICE came to me like, oh, we're using MobX. And you just don't know. That's that's that's the fun part is. And you're like, what?
[00:15:41] Michael Chan: That is pretty weird. What are some of the most surprising places that you've seen it used or like heard of it being used?
[00:15:50] Michel Weststrate: Um, that's, I think, by far uh in the Battlefield games.
[00:15:56] Michael Chan: Oh, wow.
[00:15:57] Michel Weststrate: Uh, it's just in uh in Battlefield for the in-game UI.
[00:16:03] Michael Chan: That's incredible.
[00:16:04] Michel Weststrate: And that's especially incredible because um they're not even using like React DOM or something. They have their own custom renderer for React. But they're still using MobX to make sure that those uh custom renderers uh get uh update more efficiently as they were able to pull off without. Yeah, that's by far the most fancy application uh I know of.
[00:16:27] Michael Chan: It still breaks my brain that React is being used in things like Battlefield.
[00:16:34] Michel Weststrate: Yes. Yes, that was my first uh thought as well. Like, you are using Webex?
[00:16:44] Michael Chan: Yeah, it feels like we've kind of like come full circle, right? Because like React was kind of brought out of this idea of like uh game UI engines and and now React is kind of inserting itself back into, you know, games like Battlefield, which is nuts.
[00:16:58] Michel Weststrate: Yes, but I also think it's like what uh makers of React had in mind the whole time. Because there has always been this support for custom renderers and Yeah. The fact that they've proven it continuously with React Native that it doesn't have to be the DOM makes it clear that they're usually thinking further ahead than we are.
[00:17:20] Michael Chan: Yeah, yeah, yeah. Now I thought that in particularly with um seeing all the stuff that came out this this month with, uh or I guess last month, with um with Hooks and Suspense, um what was your reaction to that, just speaking of of of them being so many steps ahead of where we're at.
[00:17:41] Michel Weststrate: So in regard to uh Suspense and concurrent uh rendering, um so so we betted 3 years ago uh on React, basically, because like every stack you're going in for a long-term project is basically a bet.
[00:17:57] Michael Chan: Yeah.
[00:17:58] Michel Weststrate: And it's like the fact that they keep pushing those kind of concepts, which no other framework is even trying, that's uh every time that confirms the initial bet for me. So Suspense is for me just a confirmation of that. I I didn't work as uh with it that much, so I didn't need it that much. Um I see which problems it solves, but the fact that it is happening itself, it's like why I keep working primarily with React and not-
[00:18:30] Michael Chan: Mm-hmm.
[00:18:31] Michel Weststrate: Uh spin-off or a framework that might be easier to set up like Vue or something. But the fact that these these kind of things are happening all the time, specifically in the React space, that's why I'm basically thrilled to be and stay in this uh community.
[00:18:49] Michael Chan: Yeah, absolutely agree. It's it was there was kind of a long period of time there in the middle where they were working through all of the Um, but Fiber and, you know, setting up for concurrent React. Um, but now that that seems stable, the amount of stuff that they're introducing just seems wild and absolutely needed for the the UI space. What are you what are you more excited about at the moment? The uh the hooks proposal or or the the Suspense stuff?
[00:19:23] Michel Weststrate: That's a hard question. From a practical perspective, I'm super hyped about hooks because they- okay, so my first thought was like, um, this is ugly.
[00:19:40] Michael Chan: Yep, yep.
[00:19:45] Michel Weststrate: My second thought was like, This is a little bit of ugly, like in the Remix way of ugly. And the third thought was like, I tried to build some things with it and like, oh, this solves so many problems.
[00:20:03] Michael Chan: Yeah. Yeah. So what are some of those problems in your mind that it that it solves for like you and your teams?
[00:20:11] Michel Weststrate: The most important thing for me is like the the composition, which is way better. And that is actually a result of the fact that you can now have um isolated state and isolated lifecycle hooks within a single component. So you either need higher-order components for that to like have that isolation preserved uh or do other weird stuff. So so for example, I uh wrote MobX React bindings uh as experiments. Uh I rewrote them in hooks.
[00:20:44] Michael Chan: Oh, wow.
[00:20:45] Michel Weststrate: And that's, so there's actually now a package, uh maybe some somebody else, MobX React Lite, which uh took that code and turned it into a uh package. But it's it's so much clearer. So in traditional bindings, it's always like fighting with, I need some state variables, need to be on state, uh but they shouldn't collide with other stuff, and I needed to do some wrapping around render and it was all hacky and monkey patching stuff. Sure. I'm needing a lot of symbols to get it all uh separated. And with hooks, it was just 10, 15 lines of code and it does basically everything. And it's like all clean and made so much more sense. And it's it's hard to express because hooks don't really enable something new in React. So so that's the weird part. But just the isolation of uh state was especially so important for me.
[00:21:44] Michael Chan: Yeah. Yeah, that is the weirdest thing about hooks is is that they seem- it's not something new, right? It's not a new concept, really. It's just a new execution that makes everything so much easier and everything so much more isolatable. Uh it's it's just wild. Like it was it feels like it was there all the time. It just needed to be uncovered, which is weird.
[00:22:12] Michel Weststrate: Yeah. There are so smart such smart decisions in some of the tiny tiny details uh and hooks. Like, usually I saw a lot of complex components with heads. Complicated components will receive props, which need to execute side effects, et cetera, et cetera. And I noticed that when rewriting them, um, that array that they they put in a second argument where you can say like, okay, these are the props and pieces of state this effect effectively depends on, um, entirely removes all that boilerplate, which is really hard to get right.
[00:22:54] Michael Chan: Because that's that's a lot of times where the errors live, right? Like is is mistakes in logic in that portion of the code.
[00:23:01] Michel Weststrate: Exactly. It's usually not in the in the side effects themselves. It's in like what should happen if new properties comes in.
[00:23:08] Michael Chan: Yeah. So, what does something like hooks mean for um for MobX long term? Like do you think that that'll be the primary mechanism for um using a library like MobX?
[00:23:22] Michel Weststrate: I think it will push uh MobX a little bit more uh outside of the components. Like we'll be able to manage state outside of components. But because it was syntactically so convenient, um people that were using MobX outside already usually just started to use MobX for local state as well. Because it was more convenient to work with uh than setState. And that's no longer the case with hooks. So I think in that sense, things will be more clearly and more cleanly separated.
[00:23:55] Michael Chan: Okay.
[00:23:57] Michel Weststrate: I think that's a good thing.
[00:23:57] Michael Chan: Yeah. Yeah, I think that is one thing that people have historically had a hard time determining with state management libraries is what should be kept local and what should be propagated up to kind of that that state management library.
[00:24:14] Michel Weststrate: Yeah, I usually have the the the rule of thumb, um, if your computer uh crashes or if your browser crashes, what state do you still want to have? Um, and usually the things you still want to have after your uh browser crashed, that's usually the state, um, it's very rule of thumb to put outside.
[00:24:35] Michael Chan: Yeah.
[00:24:36] Michel Weststrate: And usually all the rest can be component state.
[00:24:38] Michael Chan: That's really interesting. It's it's nice that now it's a lot easier to decide to use component state? Because I imagine before, um, there wasn't a lot of motivation to keep something local to a component if it was easier to put into MobX.
[00:24:53] Michel Weststrate: Well, um, no, that's not entirely true because um, with MobX you could still have it locally in the component. So it wasn't part of uh of state, but it was still part of the component itself, just as uh instance fields.
[00:25:09] Michael Chan: I see.
[00:25:10] Michel Weststrate: I see. So so that's uh was less of a problem than, for example, in a Redux where-
[00:25:15] Michael Chan: With Redux.
[00:25:15] Michel Weststrate: You could have only one store.
[00:25:17] Michael Chan: Sure, sure, sure. That makes sense. Makes sense. Now, does um does the addition of Suspense and the way that they're suspending rendering, does that have any major impact on um on MobX?
[00:25:30] Michel Weststrate: Um yes and no. I did experiment a lot with it. I thought a little about a little about it. So if you want to leverage uh Suspense, you can't use MobX for uh local component state, because um MobX will um use normal priority updates to force a component to update, uh and won't suspend it. But well, that's basically also like the effect of hooks as well. So in that sense, it aligns, and MobX should become more clearly for things outside components. And I expect it won't have much impact on state that lives uh outside components. Because for state outside components, it's anyway the case that the single source of truth is basically the model you have outside your component tree. So in that sense, you don't have any uh conflicting updates which are scheduled separately because your domain state is always ever in only one state, which is unlike UI state, which can be indeed be forked and living in separate states at the same time.
[00:26:36] Michael Chan: That's awesome. That's good to know. So, one thing that I really wanted to kind of chat with you about today is this um this other library that you're working on that's really kind of started to gain a tremendous amount of popularity um in a space that has already had kind of a lot of libraries, but none of them have really been a runaway success. And um that is Immer. Um which I think you introduced a little bit earlier this year. Is that right?
[00:27:07] Michel Weststrate: Yeah, I think I introduced it in January or February. February.
[00:27:13] Michael Chan: Okay. Cool. Yeah, so, one of the one of the most interesting tweets that I've seen on Immer is um Is by uh Sebastien Marc Boguet. He says, uh if you like MobX, I highly recommend following along M. Woestrada's work on Immer. While MobX is is pretty far removed from the vision for where we're going in React, Immer is dead on. Which is really interesting because I think that there has been a lot of- MobX has kind of filled a part of the React component that has been really hard to deal with. That idea of like asynchronous um set states and and whatnot. So, something that's kind of it kind of solved the problem from a totally different idea of like where the default React component model is. Um, but it is really interesting that they see Immer as being like dead on with the vision for React. So, can you tell me a little bit about what Immer does and why you think that it might be more aligned with the vision of React?
[00:28:13] Michel Weststrate: So, Immer helps um With writing immutable updates. And so that's why it's perfectly uh in line with the vision of React, especially for local state, where you want those um updates to be immutable, to be able to do all the suspends and scheduling and uh forking of state and replaying of state, uh etc. So that's what it uh helps with. And the reason why it is so popular are getting so much attention is basically is that it makes writing those updates really trivial.
[00:28:48] Michael Chan: Okay, and how does it do that?
[00:28:50] Michel Weststrate: So so the big difference is with most other libraries is that um it uses uh the normal JavaScript APIs.
[00:28:57] Michael Chan: Okay.
[00:28:58] Michel Weststrate: And it works with temporal mutability.
[00:29:01] Michael Chan: Okay.
[00:29:02] Michel Weststrate: And um I can use a uh a metaphor maybe.
[00:29:07] Michael Chan: Oh yes, a metaphor, please do. Those are the best.
[00:29:11] Michel Weststrate: Exactly. Um, so again, back uh at the high school, we had some of uh the teachers which used— um, I hope it's the correct word in English— uh overhead projectors.
[00:29:25] Michael Chan: Yeah, yeah, yeah, absolutely.
[00:29:27] Michel Weststrate: So so where you have a transparent sheet and you project it on the wall so that everybody can see what's uh what material's in there. And what uh teachers usually did is they put a second, another transparent sheet on top of it. And that's where they start uh scribbling on. So they draw arrows to connect things and make notes and highlights, etc. And then the great thing is that um at the end of the, of the class, you could just throw away that top sheet and his original one was still uh a modified and a preserved and he could use it again for the next lesson.
[00:30:06] Michael Chan: Mm.
[00:30:06] Michel Weststrate: And that is exactly the same thing that uh Emmer does. So you give it your current state and it starts projecting this state as a draft. And then you give it a function where you can do whatever with a draft, whatever you want. So you can mutate it and uh push things on arrays and delete keys and uh everything you like. And when you look at it for the first time, it really looks weird because it seems that like you have this original state, which is supposed to be immutable, and you just start messing around with it. But then what actually happens is that that original state uh isn't touched at all, even if it even when it uh looks that way. But what it does is whenever you make a change to it, it's quickly puts a transparent sheet on top of it, like an object where the mutation actually happens. And so the original state isn't affected, but the updates are happening on uh copies of that state, which are just uh made in place. And then when the function ends, um that is the state that's being produced. It's frozen for you so that you cannot actually modify it afterwards. And you have your next immutable state.
[00:31:19] Michael Chan: That's amazing.
[00:31:21] Michel Weststrate: So the great thing is that you're working like with normal uh JavaScript structures, while in fact uh you're producing an immutable state.
[00:31:28] Michael Chan: That's incredible, 'cause I think that one thing um one thing I've seen I think in your documentation and talks is talking about this kind of new form of callback hell. I think what was your name for it?
[00:31:40] Michel Weststrate: I think it's like oh spread hell.
[00:31:42] Michael Chan: Spread hell, yeah, yeah, yeah. Where um in order to keep You we have these— if you need to make deep updates, you have to do these like kind of like deep spreads where you're spreading all this stuff and then changing just this one property and like spreading more things and change you know adding this property or kind of slicing an array in half and inserting something where you need to insert it. And there's this whole class of programming that we have to do now that is just opening up old objects and kind of inserting new values into them. And so, this really does give you- it gives you that really nice way of just being like, oh, hey, just navigate into here and add this thing to this list or kind of change the property or remove it. But still having the benefit of mutability.
[00:32:30] Michel Weststrate: Yeah.
[00:32:31] Michael Chan: That's amazing. So, how- under the hood, these structures are still shared then? They still have like a shared structure underneath?
[00:32:39] Michel Weststrate: Yes. So so the the state you're being producing uh is still strictly sharing whatever wasn't modified. So all the optimizations with use memoization tricks um based on uh object reference equality, they all still work. It's semantically exactly the same as if you would uh be uh opening the objects manually and putting the unmodified stuff uh back in.
[00:33:08] Michael Chan: That's amazing. So, what technique are you using then to um to use that? Do you have data structures that you're building um underneath that you— or kind of do you have data structures that your library is using to maintain these under the hood? Or is it all just kind of objects and arrays underneath there? What are you what are you using to keep that shared memory?
[00:33:30] Michel Weststrate: Um, still objection arrays uh underneath. Um, so you have somewhere a root of the tree, that's where you start with. And only for that root, Immer creates a proxy. And that proxy, uh whenever you ask a value for it, it can do two things. If it was never modified before, it will return the value as it was in the original state. And if it was modified, then it will return value it has uh in the modified state. And so as soon as you uh make your first modification, uh it will quickly copy the old uh state shallowly. And so that so that you have a temporal copy uh behind the scenes, and that's where the change is stored. And that process works recursively. So if if you navigate over an object, Exactly on the path where you're navigating, proxies will be created and not outside of it. And so that means um as soon as somewhere on the path where you're navigating change something, then that change will be recorded on a copy. And when the immer function ends, uh the only thing is just um walk the proxy trees as far as it exists and uh turn that into normal plain uh objects again.
[00:34:48] Michael Chan: And so you've been able to do this all like very performantly then. From what I've seen, it seems like Immer performs exceedingly well.
[00:34:57] Michel Weststrate: Well, I mean, it's still significantly slower as if you would write a manual reducer, for example. As a rule of thumb, I usually say it's like 4 times slower.
[00:35:09] Michael Chan: Okay.
[00:35:09] Michel Weststrate: But with this, it's the same as like every optimization problem. Updating the objects in JavaScript is expensive. So even if it's 4 times slower, it still costs next to nothing. So compared to like changing something in the DOM, those are the things that are expensive. It's not mutating a bunch of objects or arrays.
[00:35:36] Michael Chan: Yeah. Now I know that proxies, which you mentioned, are a big part of the implementation. Are not supported in browsers like IE 11. So how does um how does Immer handle support for um for ES5 environments?
[00:35:51] Michel Weststrate: So for ES5 environments, it has a slightly different implementation under the hood. And so there it kind of creates fake proxies, which are just normal plain objects, uh where getters and setters uh are used to detect uh updates. And the the primary downside of it is that it sometimes has to do diffing to detect if new keys were added, which you cannot detect uh in a more easy way in an ES5 environment. But uh the great thing is that in the end, semantically, it works exactly the same. Um it's just a bit slower on ES5 environments. But in most cases, we can skip the diffing, so it's actually not uh not that bad.
[00:36:33] Michael Chan: So what are some of the practical differences then between this and other libraries like Immutable.js, for example? What's the big difference in terms of the API and what that means for developers?
[00:36:48] Michel Weststrate: Yeah, so so the big difference is that Immer, besides that produce function in which you wrap your logic, basically doesn't have an object API. It's using just normal JavaScript objects and JavaScript arrays. And it doesn't have any own data structures. So that means that not only like library is uh can stay small, uh but more importantly, that's the API is familiar because everything you can use uh for normal JavaScript object generation, you can use in Immer. Um being low dash functions, being maybe object methods you monkey patch into your browser, I don't know. They would still all work the same because they're It's all your normal JavaScript primitives uh that are used under the hood. And that has two benefits. Um not only that you don't have to learn a new API, or that you don't have to convert your objects back and forth whenever you need them uh as normal JavaScript data structures. But for me personally, uh maybe the most interesting thing about this, it's it's very easy to uh type with TypeScript or Flow. Because those languages perfectly understand normal objects and uh arrays. So they also perfectly uh understand immor functions. I I think the nice thing of that is that you're basically done with what you see in some immutable uh libraries, this string queries where you write kind of query language to update objects, um which is almost impossible to statically type. In any way weird to adopt.
[00:38:24] Michael Chan: Yeah, those those are the benefits that I find so incredibly valuable about this library is that you don't have to develop a a new style of working and you don't have to set mandates for, like, what the developer's environment should look like because it already under- you know, whatever environment they're using already understands JavaScript. And so, you're really catering towards the developer experience in kind of sneaking into the environment that's already there, but adding functionality without creating new API.
[00:39:00] Michel Weststrate: Yeah.
[00:39:02] Michael Chan: That's that's that's that's absolutely amazing. And I I see very few tools do that um successfully. So, I'm just I'm really impressed with the with the approach that you've taken with this and how easy it is to kind of opt into um into this nicer way of developing UIs or kind of working with data um that doesn't require a lot of of new- it doesn't do make a lot of requirements of the developer. Um this is really, really incredible. Now, in terms of like how this actually looks, I know it's it's always kind of hard to communicate this over audio. But for someone who has written setState and kind of used that that functional form of setState where you take the old data and kind of return an object, a new object that's changed, this produce function feels pretty pretty fami- should feel pretty familiar to all React developers.
[00:40:01] Michel Weststrate: Yeah, that's that's entirely correct.
[00:40:05] Michael Chan: I um I I love in your your video you have an example where you're doing a reducer. And um and we'll link the video in the show notes. But when you're introducing this concept, you have a reducer and uh you you give just like a 3-character example of of how you can how you can change this. Um what are some of the practical ways that that code— or can you tell me about like you know your idea of like writing the worst reducer?
[00:40:38] Michel Weststrate: Yes, um, I I think it's always good, like, when developments are happen, um, that you try to zoom out and like, how does this look from a distance? And if you look at a distance at writing reducers, I mean, purely at the syntactical form of it, it's it's worse readable code than the code you write in a usable world. And so um you you you can praise uh rightfully all the benefits it has, like in terms of semantics and all the benefits it yields, um but it's just terrible code to read. I was reading some of the examples uh even in the the Redux documentation, and it's like, what is this reducer doing? And then after a while, oh, it's storing the object under its own ID. And it's it's like so hard to to to to get it out there. And so I always try to get um back at like, what is the code uh I want to write? It's what I did with MobX, and it's also what I did with Immer. Like, how do I want to write a reducer? Let's let's forget about benefits, let's think about the code I want to write, and which will be easy to read. Um and so then I was like, okay, this is how I would like to write my reducer. And then the step after it is like, okay, now we can write it that way.
[00:41:56] Michael Chan: I think that that's really great advice um for anyone is like not to start at a low level, but to start at a high level of like what do I want the experience of writing this code to feel like. And we'll figure out the implementation over time. So, now we mentioned um just objects and arrays under the hood that it it it allows you to do all that stuff. Does it work with other types of um kind of more modern structures like sets and maps? Or is that just crazy?
[00:42:22] Michel Weststrate: No, it doesn't work with uh maps and sets. It could be made to work, but when I was working with it, um, that kind of feel felt weird. And I don't know anymore. But somehow it didn't make sense. And it's the same for classes. People uh ask occasionally, like, can I use classes with Immer? And technically we could support classes because we could like clone the the prototype or preserve prototype property. But then the conceptual model gets weird because if you're talking about classes, you're thinking about classes and instances and instances that have stayed over time. And it's actually not what immutable data trees are about. They're about values, values, values, and nothing has a state and nothing has a stable identity with where you can refer to over time and then reason about it. And how chains about time, that's exactly the model it tries to migrate away from. And so I think it could be made to work. Um, but then the amount of explanation you would have to do to make sure that people stay in the right mindset while still working with classes or maps or sets, um, I think it would be horrible in terms of uh maintenance explanation. I so far tried to like scope it like If you just think about objects and values as immutable data structures, keep it that way. They're just values. They don't have states, so they don't need methods.
[00:43:54] Michael Chan: Yeah, I think one of the huge values of this library is the absolute simplicity of what you're giving people. And you're really just giving people one function, is that right?
[00:44:06] Michel Weststrate: There are a few utility functions, but yeah, all the core is in one function.
[00:44:11] Michael Chan: I I love that that idea that something that with so much power can be reduced down to to to one function. And I think that it gives people a really great opportunity to migrate towards this. Um just kind of piece by piece instead of having to like top down, you know, transition a whole app.
[00:44:31] Michel Weststrate: Yeah, that's how uh people are using it. Uh they they they're writing uh some reducers in Emir and they like it, and then they rewrite most reducers in Emir. And maybe there's one like super performance-critical reducer that does, um, needs to do thousands and thousands of updates, and just don't use Emir for that single reducer.
[00:44:53] Michael Chan: Interesting, yeah, that that ability to be able to just kind of do it where you you want that high that experience, and then kind of opt out very easily to to kind of get eke out those like tiny little performance wins is is amazing.
[00:45:09] Michel Weststrate: Yeah, yes, I I think um one criterion I often uh measure frameworks and libraries by is like how easy is it to opt out when I need to?
[00:45:21] Michael Chan: Yes.
[00:45:22] Michel Weststrate: And that's very important. Uh and it's also one of the things I used when looking at React is like, okay, if I really need to, I can still get to the DOM nodes, manipulate them, uh and do some stuff with them without messing up with React's internal state.
[00:45:38] Michael Chan: Yeah, I gave a I gave a talk just recently and I shared some advice that a friend of mine gave me really early on and it's it's that same thing. Like anytime you put a library into your code, ask yourself how easily can you take it out later when you realize that it wasn't the right fit or you need something else. And this absolutely has that quality where you can just drop it in, see if it works for you, and if not, it comes out super easy. Was that part of the design constraints that you had for it? Or was it kind of a uh happy byproduct of the model that you chose for writing it?
[00:46:15] Michel Weststrate: Like, the fun is there wasn't really a design process. I have this cool story, like I had this problem and I wanted to design a solution for it. And I did. Um, but Immer is just basically a byproduct of something else. So Immer is actually a really uh kind of stupid story. So Roughly 1 year ago or something, I kind of concluded like, okay, proxies are now uh common enough, um they're supported by all uh evergreen browsers, uh they're now quite well optimized. So I want to use uh proxies for uh MobX because it fixes some problems which are unsolvable by proxies. Uh but MobX is a a big codebase. So I wanted to to do some experimenting just with uh proxies like really understand them uh thoroughly without applying it to MobX codebase, because in the service context you're always in when you're uh experimenting. And so I was like, okay, I I can take uh 1 or 2 days and I'm going to create copy-on-write collections uh using proxies. So the copy-on-write uh collections many people in Java are familiar with, like you you make an update and then you get, as soon as you do that, you get the copy back. Uh where you can do the next state change.
[00:47:47] Michael Chan: Mm-hmm.
[00:47:48] Michel Weststrate: And so I did that with proxies, uh and it worked, and I learned a lot. Uh and after uh 1 or 2 days, I was finished. And I was I was like, okay, uh this is cool. And only at that moment it dawned on me, it was like, wait, this actually makes uh writing read users and send updates a lot simpler. And so only at that point, it's like, I was like, okay, how should the produce function precisely look? But it actually didn't change that much anymore at that time. Um, I need to fix the ES5 problem that wasn't solved at the time. Sure. But I didn't like set out to solve this problem. At a certain moment, I realized I had solved the problem. So so that's kind of the weird story behind uh behind Immer. The original use case I had in mind for Immer was like, um we had those webpack configs in our application and like for development and production and test and uh whatever. And they were almost uh almost similar, um but you had to kind of monkey patch them. Like the the third loader for CSS that had to change its second parameter or something. Mm-hmm. You got from the development setting to the production uh configuration. And so the the original thing I tried to do is like, can I write a uh library that makes that easy? Like where I can uh take one webpack config and just mutate it, all the difficult uh spreading to get to my new webpack config, uh and then uh have it. So that that that was the the first thing I was thinking about when working on Immer, and it wasn't uh reducers or setState at all.
[00:49:40] Michael Chan: That's awesome. So in light of that, has it been a huge surprise, the success that it's found?
[00:49:47] Michel Weststrate: No, not really, because at the moment I realized it, I was like, oh, I'm really onto something.
[00:49:55] Michael Chan: Yeah.
[00:49:56] Michel Weststrate: I mean, as soon as you start using it, it's like, you're like, wow, This is this is uh spot on. This really solves a problem where a lot of people are struggling with. Yeah. And I did some Googling, did somebody else do it before, and I couldn't really find anything. Uh something's a little bit related, uh but not with an API as convenient. So I was like, okay, this is going to fly.
[00:50:23] Michael Chan: Yeah, well, it's kind of funny, at least from my perspective, When you first hear about the idea, it kind of feels like, why is this necessary? Like, like, why would we- why would we make it so that we can use the mutable APIs of objects and arrays instead of just using the immutable APIs that we have now? But when you see the examples and when you start, you know, applying it to practically to the code that you have, the shape completely changes. And it's so much more- like you said, so much more readable. I know for me, it's like every time I need to like do that dance where I like slice an array and like insert a value, like I always forget every time like the- like where those numbers are supposed to be to like insert right at the right place and not be off by one every time. Um and this really just kind of allows you to use APIs that that read a lot better. Than the immutable ones.
[00:51:23] Michel Weststrate: Yes, exactly. I mean, you notice that you have to learn some of those patterns by head. Like basically, this is pattern to insert something in an array, this is the pattern to delete a property, this is the pattern. And as soon as you like start to learn patterns, then you know that you're onto boilerplate. And then somebody else has to learn the very same pattern to be able to read your code. To recognize what's happening.
[00:51:49] Michael Chan: Yeah. I love this. So you you mentioned that there wasn't a huge amount of prior art in in this space. Um but were there libraries that that gave you inspiration and kind of a trail that led you to think about this problem in this way?
[00:52:06] Michel Weststrate: Yeah. So I said um mostly by the the the copy-on-write uh concept, which is very well known, very well documented in all languages. Immutable.js has also a kind of similar function where you have like temporal mutability to uh update the data structures before it gets uh sealed again. But I don't think I wasn't wasn't looking at it too much at that time. Because I said my my my goal was not to replace Immutable.js or something, my goal was to learn proxies.
[00:52:41] Michael Chan: Yeah.
[00:52:43] Michel Weststrate: So, but I I think, yeah, mostly that the the whole field of copyright collections, that's the the prior art of this.
[00:52:49] Michael Chan: Do you have any advice to people who are just kind of getting started in open source and maybe feeling a little hesitant to share ideas and whatnot? Do you have any advice for them, kind of given your experience now in the open source space?
[00:53:04] Michel Weststrate: Yeah, a few. Um, I think for first, um, for starters, you have to learn to like ignore that things can be done or shouldn't be done. If you have an idea and you think it's good, you just have to go for it. And then, um, well, it's it's kind of standard advice, right? Um, and you have to try it a little bit, and if it feels okay-ish, then the hard part comes, because at that point you have to get other people to play with it. And for that to happen, um, something must be very must be very clear, and that's what your uh idea and your philosophy uh is behind it. And that's usually where I think uh most projects uh crash, not because they're uh not good enough or something, but because they're used selling themselves just in terms of uh it's simpler or it's faster or it's better. Like in terms of technical properties, but things that get people really hooked is like, what's the philosophy behind it? Uh, and what's the bigger picture problem you're trying to solve?
[00:54:17] Michael Chan: Yeah.
[00:54:18] Michel Weststrate: And like, I'm following the like the state management field quite closely, and usually new state management libraries appear, I think, still on a weekly basis, and I usually Uh, take at least like the effort to read the README. And with most of them, I'm just like, okay, it's cool, but I don't get what the bigger picture is. I mean, I I I I can recognize some other uh prior art in it, and I can see something which is syntactically slightly nicer. But I noticed that that the thing uh which is important is what's the bigger picture uh behind it. And uh if you can't communicate communicate that clearly, and it's a separate skill from uh DORA engineering. That helps a lot. And then it's really the the um the challenge of finding some people who want to share their experience with your stuff. Because, well, clearly you yourself are enthusiastic about the things you built yourself.
[00:55:20] Michael Chan: Mm-hmm.
[00:55:21] Michel Weststrate: But if somebody else is enthusiastic, that means that the idea uh landed and became clear, uh, it's valuable for uh all of us.
[00:55:28] Michael Chan: Yeah, I think that that is one great gauge of success is when you see other people using the library that you made to solve another problem or a a higher level problem. Um, have you seen- I know that there's a number of libraries now that are using Immer um with components or for state management. Um, are there some that seem like most interesting to you or kind of embody the philosophy best?
[00:55:58] Michel Weststrate: Two I've uh I find very interesting are uh Unstated and React Copy-on-Write. They're both quite similar, they look a lot alike. Um actually, the work on React Copy-on-Write was uh stopped because of the introduction of uh hooks. But Unstated is interesting because um it is It's small, it has a simple API, and it nicely uh embodies Immer in the bigger picture of a state tree that is stored in the in the context.
[00:56:34] Michael Chan: Very interesting. Well, it has been um it has been awesome talking with you and hearing about your your ideas and the kind of serendipitous inspiration behind Immer. Um so thank you so much for your time. Is there anything else that you want to uh kind of leave us with? Um any recommendations, any projects that you're super excited about, people that you follow that inspire you, um anything?
[00:56:59] Michel Weststrate: A great recommendation I uh always want to make is, like, if you're at a conference, make uh sure to not just attend the talks, um but find the speakers you want to talk to, and maybe you don't dare, or maybe you're afraid not to be able to- I don't know. Uh, actually, I don't know why people are afraid. Uh, I don't know this. People have to- are often hesitant to approach speakers, either ones they're familiar with or which they don't know yet. But really, uh, approach everybody you want at the conference. That's how you get uh the most value out of it.
[00:57:42] Michael Chan: That hallway track is like the most important thing. I mean, especially now that you can watch talks afterwards. Like it seems weird sometimes because now like talks are like empty and they end up just being like a video online. But yeah, like getting out there and talking with people that are building the libraries that you use and asking them questions in person and talking, you know, like we're talking right now, just about the inspiration and you know what their thought process is, is is invaluable.
[00:58:11] Michel Weststrate: Exactly, exactly. I mean, often I I miss talks which I wanted to go to because I'm still talking to somebody. And that's, I think, the way uh how it should be. Because like if you miss a talk, you can always watch it back. Uh but you can't replay a conversation you have uh in the canteen.
[00:58:31] Michael Chan: Absolutely. Well, thank you so much for your time. I really do appreciate uh you taking the time to meet with me. And I know that we uh kind of had some like scheduling things and kind of moving it around. And so, I really appreciate your time and uh just talking with us about what you're working on and um how it could really improve people's applications. So, thanks so much for your work. Thanks for listening to this episode of React Podcast. Links and notes for this show are available at reactpodcast.com/51. As you go, I hope you leave inspired to build something around a cumbersome pattern. At times, it can feel like coloring outside the lines, but that's where the great ideas are. Don't be afraid to play around and see where it goes. I'm excited for you. Thanks for listening. This episode of React Podcast was edited by Mikael Delport. It was produced by Mikael Delport and Sarah Jackson. You can find React Podcast on Speck, a network to help designers and developers level up. Visit spec.fm to find other shows that will take you further in your career. Help us out by reviewing this show on iTunes. Your reviews help the show grow and help us ensure great guests and awesome content week to week. To join the discussion. visit reactpodcast.com/chat or follow us on Twitter @reactpodcast. I'm @chantastic. To stay out of the discussion but get updates, visit reactpodcast.com/news and sign up for emails. Thanks so much for giving us your attention. We'll be in your ears again next week.
chantastic.fm