All right. Good morning, Amsterdam. My name is Marc Erickson, and I'm very excited to share with you a guide to React compiler rendering. A couple of quick things about myself. I'm a senior time-travel engineer at Replay where we built a time-travel debugger for both humans and agents. I will answer questions anywhere there is a text box on the internet. I help moderate the React communities. I collect interesting links and talk about them in places like the This Month in React podcast. I write extremely long blog posts. I recently wrote an article about my own emotional experience using AI as well as my workflow, and I am a Redux maintainer. It is almost exactly ten years that I've been working on Redux. But also most people know me as that guy with the Simpsons avatar, which is actually now slightly out of date. I had LASIK back in January.
Why are we talking about React rendering today? I am a firm believer that it is important to know fundamentals of the tools we are using, and that's even more true in an age of AI. Even today, I see a lot of people with misconceptions about how React actually works. When we talk about the React compiler, it flips some of that default behaviour upside down. You need to make sure you understand the fundamentals first. And this is really science, not magic. So the goals for today, we will spend some time reviewing some fundamentals of React behaviour, we are going to look at the options we have available for optimising React performance, we will look at what the compiler does and what the output looks like, and then we will finish by talking about how it changes our mental model of how to use React. The slides are up on my blog, blog.isquaredsoftware.com, and I will have the link there again at the end.
Back to the beginning. What is rendering? Rendering is the process of React asking your components, what do you want the UI to look like at this point in time? Normally, the render output is based on the current props and the state for that component. When we write components, we write this angle bracket JSX syntax that we have seen and all know and love, and when we write the JSX syntax in our code, at build time, that gets converted into create element calls, and then at runtime, your component executes, it calls create element, and it returns these element objects that have the component and its props and the children. If you've ever heard the term virtual DOM, that's really just describing these element objects. So during a render, React loops over all your components, it collects all the element objects in a tree, and then it diffs the old and the new element trees to figure out, are you asking for anything different in the UI this time? Now, a very, very important point, if there's anything you remember, please remember this slide right here. React's default behaviour is that when you render in one component, you call set state, React will keep recursing down through all the children below that. So if I call set state in the app component up there, it is going to continue through all four of the children below that. If I call set state in the inspiration component, it will also render the two children below that. It keeps going down through all the children below. Now, when we render, this does not always mean that there's an update to the DOM.
Comments