Don't Forget React Memo


React developers intrigue us with the so-called React Forget compiler [that’s supposed to inject useMemo and useCallback into code automatically]. However, we still can't figure out best practices for useCallback, useMemo, and React.memo when we write it on our own. The worst scenario is if we continue to put it everywhere without understanding. As a result, it can bring noticeable harm to the performance of our application. In this talk, on real-life use cases, you will see solutions to the problem of using hooks wrong or overusing them!



Okay, hi everyone. I'm so glad that you have joined me and make some little bit noise and make some interactions. Where is the noise? Yeah. So, have you ever, have you, okay, have you already seen ReactCon 2021? Please raise your hands up. Oh, so few. Okay. Just a quick look through. It has a new compiler called like ReactMemo, which is supposed to inject ReactMemo, use callback, use memo automatically in your code. It sounds really fascinating, but unfortunately, it's still a developer mode and I have no clue when it's going to be in the nearest future. So, let's back up to our daily life and I noticed that there is still a gap of understanding how use memo and use callback works. We are overusing it. About what is that memorization? And the hardest question is now how can we figure it out of the best practices? So, let me introduce myself. My name is Kristina. I'm software engineer at SoftServe and today I'm going to talk about it in details. So, when I see such issues, I'm a mathematician person, background, I just do simple things. I simplify it or prove it. And the best way to figure it out is just to forget use memo and to write its own custom hook. So, we are going to build hook from scratch. What we needed to do? First one, we should return plain JavaScript object. Its value can be accessed and modified and we shouldn't forget about that it should be persist. I need some object instead of creating a new one of subsequent render. So, I use it use state for implement this thing and its return an array and index zero is the current value. If I have started to use hooks like use state, why I cannot replace it by use ref? I can do this and it does the same thing and it's now it's more readable. So, what another thing we should add into our function? It's now it's updating over and over again but we don't need it. And it can help us shallow compare as it does in React. So, wrapping all our ideas, I create my own use memo function with value for current value and the previous one. Then I evoke shallow equals where I started to checking for is it array for its length and finally I check its for their items are new for us. If everything is okay, it returns true and then we override it and return a value. So, making like shorting something up, we can modify object not only by use ref. We can how it's interesting for me that use memo is so similar to use ref but the only difference is that it has extra condition check. And there is another question comes to me that if use callback is equivalent to use memo. And now it's not a secret and we can understand why. So, I hope after this talk, looking at tweet or looking at React documentation now, it's not only words for you. It's also you have a profound knowledge by proofs. Hooray! And now I just want to take a bonus part of it and to share my knowledge how I use use callback and use memo. First one, when you have parent component and child component. If in the parent component you use use callback, I highly recommend you to wrap child component with React memo to prevent its re-renders. Next one is when you want to create your own Hawk component or Hawk component with dependencies, I also recommend you to wrap it with use callback. If you don't do this, don't worry. Yes, hook plugin will call you back. And another example of using use memo, it's quite simple for optimizing expensive calculations such as filtering, sorting and formatting. And the great example is React table does. And the last slide is dedicated to my country. We still need the support. We still need some donates and amuse. Here you can find further information and don't be afraid to ask Ukrainian any questions you have. And here if you want to find me in Google, you just can write my nickname and you will follow to my Twitter and GitHub. I will be really appreciative to you if you join me. Thank you so much.
7 min
17 Jun, 2022

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

Workshops on related topic