I'm not deep understanding how memo works with the component. I have component like this. It is very simple and it is static.
const Home = memo((props: any) => {
useEffect(() => {
console.log('home changed');
}, [])
return (
<>
<h1>This is homepage</h1>
</>
)
})
I has route is /home. Everytime I come to Homepage route, I always got message with log home change. So this component is not cached with memo?
If it was cached, how to realize the Component is not re-rendered?
memocan't stop the very first render which happens when mounting a component. That render must always take place. So if you route away from the home page you unmount it, and when you route back you must re-mount it, resulting in one renderInstead what
memodoes is potentially skip later renders. Every time the component would re-render due to a change in props, react will first do a shallow comparison of all the props. If none of them changed, it skips rendering and uses what your component returned last time.In order for
memoto work, the props need to stay the same, so you will need to make sure you're not changing props. Functions are indeed one case you need to be careful of, because if you're making a brand new function every time you render and then passing that to a child component, the child is constantly having its memoization broken. The fix for this is typically to wrap your function inuseCallbackso you use the same instance of the function on each render. For example: