React re-renders guide: preventing unnecessary re-renders

This is the second part of React re-renders guide: everything, all at once. There are no investigations, no explorations in this guide, just raw facts and LOTs of patterns. Each pattern is linkable independently and has:

​- a short description
- visual aid that demonstrates the pattern
- working code example in codesandbox
- links to deep dives where necessary

The intention here was to create something that can be referenced on a regular basis when writing components or fixing performance problems. Or shared with a struggling teammate as a link to an exact pattern. Or maybe even printed and hang on walls so it’s always in front of the eyes 😄

The full guide table of content:

Preventing re-renders with composition

⛔️ Antipattern: Creating components in render function

Creating components inside render function of another component is an anti-pattern that can be the biggest performance killer. On every re-render React will re-mount this component (i.e. destroy it and re-create it from scratch), which is going to be much slower than a normal re-render. On top of that, this will lead to such bugs as:

  • possible “flashes” of content during re-renders
  • state being reset in the component with every re-render
  • useEffect with no dependencies triggered on every re-render
  • if a component was focused, focus will be lost

See example in codesandbox

Additional resources to read: How to write performant React code: rules, patterns, do’s and don’ts

✅ Preventing re-renders with composition: moving state down

This pattern can be beneficial when a heavy component manages state, and this state is only used on a small isolated portion of the render tree. A typical example would be opening/closing a dialog with a button click in a complicated component that renders a significant portion of a page.

In this case, the state that controls modal dialog appearance, dialog itself, and the button that triggers the update can be encapsulated in a smaller component. As a result, the bigger component won’t re-render on those state changes.

See example in codesandbox

Additional resources to read: The mystery of React Element, children, parents and re-renders, How to write performant React code: rules, patterns, do’s and don’ts

✅ Preventing re-renders with composition: children as props

This can also be called “wrap state around children”. This pattern is similar to “moving state down”: it encapsulates state changes in a smaller component. The difference here is that state is used on an element that wraps a slow portion of the render tree, so it can’t be extracted that easily. A typical example would be onScroll or onMouseMove callbacks attached to the root element of a component.

In this situation, state management and components that use that state can be extracted into a smaller component, and the slow component can be passed to it as children. From the smaller component perspective children are just prop, so they will not be affected by the state change and therefore won’t re-render.

See example in codesandbox

Additional resources to read: The mystery of React Element, children, parents and re-renders

✅ Preventing re-renders with composition: components as props

Pretty much the same as the previous pattern, with the same behavior: it encapsulates the state inside a smaller component, and heavy components are passed to it as props. Props are not affected by the state change, so heavy components won’t re-render.

Can be useful when a few heavy components are independent from the state, but can’t be extracted as children as a group.

See example in codesandbox

Read more about passing components as props here: React component as prop: the right way™️, The mystery of React Element, children, parents and re-renders

Preventing re-renders with React.memo

Wrapping a component in React.memo will stop the downstream chain of re-renders that is triggered somewhere up the render tree, unless this component’s props have changed.

This can be useful when rendering a heavy component that is not dependent on the source of re-renders (i.e. state, changed data).

See example in codesandbox

✅ React.memo: component with props

All props that are not primitive values have to be memoized for React.memo to work

See example in codesandbox

✅ React.memo: components as props or children

React.memo has to be applied to the elements passed as children/props. Memoizing the parent component will not work: children and props will be objects, so they will change with every re-render.

See here for more details on how memoization works for children/parent relationships: The mystery of React Element, children, parents and re-renders

See example in codesandbox

Improving re-renders performance with useMemo/useCallback

⛔️ Antipattern: unnecessary useMemo/useCallback on props

Memoizing props by themselves will not prevent re-renders of a child component. If a parent component re-renders, it will trigger re-render of a child component regardless of its props.

See example in codesandbox

✅ Necessary useMemo/useCallback

If a child component is wrapped in React.memo, all props that are not primitive values have to be memoized

See example in codesandbox

If a component uses non-primitive value as a dependency in hooks like useEffect, useMemo, useCallback, it should be memoized.

See example in codesandbox

✅ useMemo for expensive calculations

One of the use cases for useMemo is to avoid expensive calculations on every re-render.

useMemo has its cost (consumes a bit of memory and makes initial render slightly slower), so it should not be used for every calculation. In React, mounting and updating components will be the most expensive calculation in most cases (unless you’re actually calculating prime numbers, which you shouldn’t do on the frontend anyway).

As a result, the typical use case for useMemo would be to memoize React elements. Usually parts of an existing render tree or results of generated render tree, like a map function that returns new elements.

The cost of “pure” javascript operations like sorting or filtering an array is usually negligible, compare to components updates.

See example in codesandbox

Improving re-render performance of lists

In addition to the regular re-renders rules and patterns, the key attribute can affect the performance of lists in React.

Important: just providing key attribute will not improve lists' performance. To prevent re-renders of list elements you need to wrap them in React.memo and follow all of its best practices.

Value in key should be a string, that is consistent between re-renders for every element in the list. Typically, item’s id or array’s index is used for that.

It is okay to use array’s index as key, if the list is static, i.e. elements are not added/removed/inserted/re-ordered.

Using array’s index on dynamic lists can lead to:

  • bugs if items have state or any uncontrolled elements (like form inputs)
  • degraded performance if items are wrapped in React.memo

Read about this in more details here: React key attribute: best practices for performant lists.

See example in codesandbox — static list

See example in codesandbox — dynaminc list

⛔️ Antipattern: random value as key in lists

Randomly generated values should never be used as values in key attribute in lists. They will lead to React re-mounting items on every re-render, which will lead to:

  • very poor performance of the list
  • bugs if items have state or any uncontrolled elements (like form inputs)

See example in codesandbox

Preventing re-renders caused by Context

✅ Preventing Context re-renders: memoizing Provider value

If Context Provider is placed not at the very root of the app, and there is a possibility it can re-render itself because of changes in its ancestors, its value should be memoized.

See example in codesandbox

✅ Preventing Context re-renders: splitting data and API

If in Context there is a combination of data and API (getters and setters) they can be split into different Providers under the same component. That way, components that use API only won’t re-render when the data changes.

Read more about this pattern here: How to write performant React apps with Context

See example in codesandbox

✅ Preventing Context re-renders: splitting data into chunks

If Context manages a few independent data chunks, they can be split into smaller providers under the same provider. That way, only consumers of changed chunk will re-render.

Read more about this pattern here: How to write performant React apps with Context

See example in codesandbox

✅ Preventing Context re-renders: Context selectors

There is no way to prevent a component that uses a portion of Context value from re-rendering, even if the used piece of data hasn’t changed, even with useMemo hook.

Context selectors, however, could be faked with the use of higher-order components and React.memo.

Read more about this pattern here: Higher-Order Components in React Hooks era

See example in codesandbox

The full guide is published at https://www.developerway.com. The website has more articles like this 😉

Subscribe to the newsletter, connect on LinkedIn or follow on Twitter to get notified as soon as the next article comes out.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Nadia Makarevich

Nadia Makarevich

768 Followers

Frontend architect, coder. Love solving problems, fixing things and writing in-depth tech articles: https://www.developerway.com