So there is an opportunity to cache this JSX as well. So there are three things to cache. That's why it is three. Now this dollar will have three elements, dollar of 0, 1, 2. In all these elements we can keep the cachable entries.
Now here it is checking whether heading or total products, any of these things are changing or not. In case there is a change, here you have to see, in case there is a change, then what we are doing? We are computing the fresh JSX part with the heading and the total products. And then we are doing dollar zero equals to heading this particular array, dollar one equals to total products. We are basically caching it, keeping it. Dollar two is this computed JSX. If it is not changing, then just return dollar two, which is nothing but the computed JSX. So it means if your heading and total products are not changing, you are always returning what was previously computed. That's all it is doing. Very simple, memoized block, that block to compute something based on the dependencies that you are passing it to. Isn't it?
Now, the last few things that I want to call out again about this one is a bit, some tips and tricks that you have to know. For example, in the code, I have showed you that I am able to configure this React compiler for the entire project, right? As I'm doing in the vid.config.js file. But you always have an opportunity for not doing this for the entire project. You can skip it, you know? So for that, don't do this, you know, and then we're doing this, you have to pass a few more configurations. This is where this React compiler config will come into picture. What you can do instead of keeping it empty, you can now set a compilation mode, something like this.
You can say compilation mode is annotation. And after that, the component that you are interested to optimize, for example, this heading.jsx, over here at the top of your component where the component starts, you can do something like useMemo. So it means your compiler now will identify what are those components that is having this useMemo directive, and lets optimize only those components because I have selected for the annotation mode. In this case, only heading component will be optimized. Most of the components, product list, you know, feature product is not going to be optimized. There is also another directive called useNomemo. You write it like this. This is to say that, this is to say that, you know, don't optimize this particular component at all. Even though I have annotated, but don't optimize.
Comments