Video Summary and Transcription
The Talk covers topics such as React memo compiler, use memo and use callback, building custom hooks, and the benefits of using use ref. It also explores the similarities between use memo and use ref, and the use of React Memo for preventing re-renders and optimizing expensive calculations. The speaker also shares personal information and contact details.
1. Introduction to React Memo and Custom Hooks
I'm going to talk about the React memo compiler, the gap in understanding how use memo and use callback works, and the best practices. I will also discuss building a custom hook and the benefits of using use ref instead of use state. Finally, I will introduce my own use memo function with shallow compare.
Okay, hi, everyone. I'm so glad that you have joined me and make some little bit noise. Let's make some interactions. Where is the noise? Yeah. So, have you ever – okay. Have you already seen React Conf 2021? Please raise your hands up. Oh, so few. Okay. Just a quick look through. It has a new compiler called React memo, which is supposed to inject React memo, 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 notice 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 the best practices? So, let me introduce myself. My name is Kristina. I'm software engineer at SoftSurf. 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 approve 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 need 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 the subsequent render. So, I use it's used state for implement this thing and its return an array and index 0 is a current value. If I have started to use hooks like used state, why I cannot replace it by use ref? I can do this and it does the same thing and it's more readable. So, what another thing we should add into our function? It's updating over and over again but we don't need it. And it can help us shallow compare as does it in React. So, wrapping all our ideas, I create my own use memo function with value for current value and the previous one.
2. Exploring use Memo, use Callback, and React Memo
I discuss the similarities between use memo and use ref, and the extra condition check in use memo. I also share my knowledge on using use callback and use memo, such as wrapping child components with React Memo to prevent re-renders and creating Howk components with dependencies. Additionally, I mention the use of use memo for optimizing expensive calculations like filtering and sorting. Lastly, I dedicate a slide to my country and provide information on how to find me on Google, Twitter, and Github.
Then I evoke shallow equals where I started checking for is it array for its length and finally I check it 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. How was 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 call back 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 it's not only words for you. It's also you have a profound knowledge by proves. 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 parent component you use use callback I highly recommend you to wrap child component with React Memo to prevent its re-renders. And next one is when you want to create your own Howk component with dependencies. I also recommend you to wrap it with use callback. If you don't do this, don't worry! Yes, Howk 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 fomenting. And the great example is React table does.
And the last slide is dedicated to my country. We still need a support. We still need some donates and ammunition. 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, in Google, you just can write my nickname and you will follow to my Twitter and Github. I will be really appreciative of you if you join me.
Comments