18 Oct 2021
A Visual Guide to React Rendering - Cheat Sheet
When does a React component re-render? What can cause re-renders, and how to prevent unnecessary renders? Here is a quick cheat sheet you can refer to whenever you find yourself asking these questions.
This article serves as a table of contents for the Visual Guide to React Rendering series. Each section of the cheat sheet links to the corresponding chapter of the guide that explores the topic in depth.
Standard rendering and memo
By default, when the state of a component changes, that component and all its children re-render. You can wrap a React component with memo
to prevent an entire subtree from re-rendering.
A Visual Guide to React Rendering - It Always Re-renders (Chapter 1)
Primitive vs Non-primitive props
Non-primitive values in JavaScript are compared by reference.
Keep this in mind when passing props to memoized components. Memoized components re-render when their props change.
A Visual Guide to React Rendering - Props (Chapter 2)
Stable reference with useMemo
You can preserve a reference to a non-primitive value with useMemo
. It won't change on re-renders.
A Visual Guide to React Rendering - useMemo (Chapter 3)
Stable reference with useCallback
You can preserve a reference to a function with useCallback
.
A Visual Guide to React Rendering - useCallback (Chapter 4)
Rendering and Context
The component right under your context provider should probably use memo
A Visual Guide to React Rendering - Context (Chapter 5)
Rendering and DOM
React component render does not mean DOM update. React is smart enough to update only those parts of the DOM that need to change.
A Visual Guide to React Rendering - DOM (Chapter 6)
Articles on the topic:
- A (Mostly) Complete Guide to React Rendering Behavior - Mark Erikson
- Fix the slow render before you fix the re-render - Kent C. Dodds
- Before You memo() - Dan Abramov