Also, I'm just going to point out, if you're using React 19 or higher, there's a plugin called React Compiler that might actually handle all of this for you. I didn't know about this at the time of this project, so I didn't get to use it. I do feel like it's worth pointing out because it's supposed to automatically handle all of these decisions. I wouldn't be surprised if this section was completely obsolete in a few years.
The syntax for memo is super easy. You basically just have your component, and when you export it, just wrap it in memo. You can wrap it however you like, but this is the easiest way for me to do it. Again, it's so easy, you'd be tempted to do it everywhere, but be careful.
Let's talk about some useful debugging tools because it's never going to work on the first try. The first place you're going to look, of course, is Chrome's DevTools. The memory tab, there's lots of stuff in here. I'm certainly not an expert on it. If you are just starting out in the profiling world, like I was, I would go to the allocations on the timeline section, I would hit record for a few seconds, let it run, and then you'd get something that looks like this.
Chrome has an FPS palette. If you open DevTools and hit Control-Shift-P, same command as in VS Code to open up the command palette, you can actually just type in FPS, and this little box will pop up, and it's wonderful. It tells you any time your animation is slowing down. This also shows us that we are getting our GPU acceleration, and that we've got plenty of memory left. Finally, if all else fails, you've got console.time and time.end. People like to make fun of front-end devs for overusing console logs, but they work, and console.time and time.end are like the weird cousin of console.log.
Hey, you guys, mission accomplished. We did it. The client is happy. The museum is happy. Children are having their faces scanned, consensually, I might add. And we've learned something about animating React. So I hope this was fun. Please reach out if you have any questions or comments and don't forget to download the slides and the source code here. Thank you so much.
Comments