React.memo, useMemo, and useCallback Optimizations

Pubblicato il: 13 novembre 2019
sul canale di: Code Bushi
44,852
1.5k

Learn how to optimize React Components using React.memo, useMemo, and useCallback hooks.

Key Takeaways!

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, 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-a...

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

Follow me on
Twitter:   / huntarosan  
Dev.to: https://dev.to/changoman


In questa pagina del sito puoi guardare il video online React.memo, useMemo, and useCallback Optimizations della durata di ore minuti seconda in buona qualità , che l'utente ha caricato Code Bushi 13 novembre 2019, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 44,852 volte e gli è piaciuto 1.5 mille spettatori. Buona visione!