Current section: Optimize Rendering 33 exercises
Problem

Primitives

Loading exercise

Transcript

00:00 Custom comparators are fun and all, but it would be great if we could make it so we don't need a custom comparator. And if you change the way that you have your component accepting props, then you can do that by changing to only accept primitive values. And then the default memoization can take hold.

00:17 So, if you recall our avatar example, we're comparing the avatar URL and the name and whether those changed, and that determines whether our UI is going to update. But, you know, the built-in memoization comparator is just going to compare the props. Are they triple equal?

00:37 And so, if instead of accepting a user that could have various things on it change, or the object itself could change, if instead of that, we actually make our memo, or our memoized component, accept primitive values,

00:53 then the built-in comparator that memo does will work for this. And so here we have our avatar URL and our name, and now we don't need to worry about whether other properties of the users changed or anything like that. It's just these properties are the ones we depend on.

01:09 These are the primitives, and you go ahead and do your own comparison there. And if you need to have some other thing, like their face, I don't know. Yeah, sure. Thanks, AI assistant. Then that also can be another primitive value.

01:27 We don't have to worry about breaking memoization, and everything will work much nicer. So, in this exercise, we're going to be doing that to improve the performance of our memoization, or rather preserve the performance of our memoization,

01:44 while also making the code a lot, lot simpler. So, let's get into it.