Current section: Optimize Context 33 exercises
Problem

Memoize Context

Loading exercise

Transcript

00:00 So now our example has this idea of a footer context that accepts both the color and the name. So both of these elements of state are in the context provider, and the footer will consume that. And the optimization that we want to make now is if you change the state for the app count,

00:18 that shouldn't re-render the footer because the footer doesn't care about the app count. It only cares about the color and the name. But what we're observing is not that. We are observing that when you increment the app count, the footer is going to re-render unnecessarily. So let's check that out.

00:37 Increment the app count and turn that off. And we will see that the footer is indeed getting re-rendered. We have source maps and some weird things, so this is called footer 2. This is our memoized footer component.

00:51 So that is re-rendering because the context changed, even though the individual values of context did not change. So we need to memoize this. I want you to first, before you start, pull this up and run that. Make sure you're seeing the same thing.

01:05 And then go ahead and implement the use memo around this thing and check it again. Like most of the performance optimizations, we're doing not a lot of code changes, but I want you to be spending time in the DevTools here anyway. So see you when you're done.