React.memo, useMemo, and useCallback for Performance Optimizations in React

Key Takeaways From The Video:

4:40 - When a Parent Component re-renders, all Child components will re-render as well.

5:30 - React.memo is a higher order component, used with functional components. React.memo will compare the previous props to the next props, and if different, will re-render the component.

6:52 - JavaScript Primitives (string, number, boolean) being passed as a prop will work fine with React.memo.

7:30 - Arrays, Objects, and Functions being passed as a prop will not work with React.memo alone. You'll need to use hooks like useMemo around the Array or Object.

8:17 - In a functional component, every re-render will cause everything in the function body to be "re-created". So Arrays, Objects, and Functions will be new and different on every re-render. When these are passes to the Child Component, it will cause the Child to re-render.

8:55 - Wrapping Arrays and Objects with the useMemo hook will solve this problem. Better yet, if the dependency array is empty, you should just hoist it out of the function body.

14:24 - When passing Functions down as props, and when Functions are used in a dependency array, wrap them in the useCallback hook to avoid re-renders and prevent useEffects from firing on each re-render.

20:30 - Don't just memoize all the things. Here is the blog post by Kent about when you should use these memoization techniques: https://kentcdodds.com/blog/usememo-and-usecallback

Finished Code: https://github.com/codebushi/react-memo

If you like the YouTube content, please support it by subscribing to the channel!

Learn JavaScript and React

Here are some of my favorite courses for learning JavaScript and React. I have personally purchased each of these and found them extremely helpful when I was learning JavaScript and React. Check out the resources page for more.