Tuesday, March 14, 2023
HomeReactWhy React Re-Renders

Why React Re-Renders


So, I will be sincere. I had been working professionally with React for years with out actually understanding how React’s re-rendering course of labored. 😅

I feel that is true for many React builders. We perceive sufficient to get by, however in case you ask a gaggle of React builders a query like “What triggers a re-render in React?”, you will doubtless get a handful of various hand-wavy solutions.

There are plenty of misconceptions on the market about this subject, and it could actually result in plenty of uncertainty. If we do not perceive React’s render cycle, how can we perceive tips on how to use React.memo, or after we ought to wrap our capabilities in useCallback??

On this tutorial, we’ll construct a psychological mannequin for when and why React re-renders. We’ll additionally discover ways to inform why a particular part re-rendered, utilizing the React devtools.

So, let’s begin with a elementary reality: Each re-render in React begins with a state change. It is the one “set off” in React for a part to re-render.

Now, that most likely does not sound correct… in any case, do not elements re-render when their props change? What about context??

Here is the factor: when a part re-renders, it additionally re-renders all of its descendants.

Let’s take a look at an instance:

Code Playground

import React from 'react';

perform App() {
  return (
      <Counter />
        <p>Copyright 2022 Huge Rely Inc.</p>

perform Counter() {
  const [count, setCount] = React.useState(0);
  return (
      <BigCountNumber rely={rely} />
      <button onClick={() => setCount(rely + 1)}>

perform BigCountNumber({ rely }) {
  return (
      <span className="prefix">Rely:</span>

export default App;

On this instance, we have now 3 elements: App on the prime, which renders Counter, which renders BigCountNumber.

In React, each state variable is connected to a specific part occasion. On this instance, we have now a single piece of state, rely, which is related to the Counter part.

Every time this state modifications, Counter re-renders. And since BigCountNumber is being rendered by Counter, it too will re-render.

Here is an interactive graph that exhibits this mechanic in motion. Click on the “Increment” button to set off a state change:


Props: { rely }

(The inexperienced flash signifies {that a} part is re-rendering.)

Alright, let’s clear away Huge False impression #1: The complete app re-renders each time a state variable modifications.

I do know some builders imagine that each state change in React forces an application-wide render, however this is not true. Re-renders solely have an effect on the part that owns the state + its descendants (if any). The App part, on this instance, does not need to re-render when the rely state variable modifications.

Reasonably than memorize this as a rule, although, let’s take a step again and see if we will work out why it really works this manner.

React’s “principal job” is to maintain the applying UI in sync with the React state. The purpose of a re-render is to work out what wants to vary.

Let’s take into account the “Counter” instance above. When the applying first mounts, React renders all of our elements and comes up with the next sketch for what the DOM ought to seem like:

When the person clicks on the button, the rely state variable flips from 0 to 1. How does this have an effect on the UI? Effectively, that is what we hope to study from doing one other render!

React re-runs the code for the Counter and BigCountNumber elements, and we generate a brand new sketch of the DOM we wish:

Every render is a snapshot, like a photograph taken by a digicam, that exhibits what the UI ought to seem like, primarily based on the present software state.

React performs a “discover the variations” recreation to determine what’s modified between these two snapshots. On this case, it sees that our paragraph has a textual content node that modified from 0 to 1, and so it edits the textual content node to match the snapshot. Happy that its work is completed, React settles again and waits for the following state change.

That is the core React loop.

With this framing in thoughts, let’s look once more at our render graph:


Props: { rely }

Our rely state is related to the Counter part. As a result of knowledge cannot circulation “up” in a React software, we all know that this state change cannot probably have an effect on <App />. And so we need not re-render that part.

However we do must re-render Counter‘s baby, BigCountNumber. That is the part that really shows the rely state. If we do not render it, we can’t know that our paragraph’s textual content node ought to change from 0 to 1. We have to embody this part in our sketch.

The purpose of a re-render is to determine how a state change ought to have an effect on the person interface. And so we have to re-render all potentially-affected elements, to get an correct snapshot.

Alright, let’s speak about Huge False impression #2: A part will re-render as a result of its props change.

Let’s discover with an up to date instance.

Within the code beneath, our “Counter” app has been given a model new part, Ornament:

Code Playground

import React from 'react';

import Ornament from './Ornament';
import BigCountNumber from './BigCountNumber';

perform Counter() {
  const [count, setCount] = React.useState(0);
  return (
      <BigCountNumber rely={rely} />
      <button onClick={() => setCount(rely + 1)}>
      <Ornament />

export default Counter;

(It was getting a bit crowded, having the entire elements in a single huge file, so I took the freedom of re-organizing. However the general part construction is similar, except for the brand new Ornament part.)

Our counter now has a cute lil’ sailboat within the nook, rendered by the Ornament part. It does not rely on rely, so it most likely will not re-render when rely modifications, proper?

Effectively, er, not fairly.


Props: { rely }

When a part re-renders, it tries to re-render all descendants, no matter whether or not they’re being handed a specific state variable by way of props or not.

Now, this appears counter-intuitive… If we aren’t passing rely as a prop to <Ornament>, why wouldn’t it must re-render??

Here is the reply: it is exhausting for React to know, with 100% certainty, whether or not <Ornament> relies upon, immediately or not directly, on the rely state variable.

In a really perfect world, React elements would at all times be “pure”. A pure part is one which at all times produces the identical UI when given the identical props.

In the actual world, a lot of our elements are impure. It is surprisingly simple to create an impure part:

This part will show a distinct worth each time it is rendered, because it depends on the present time!

A sneakier model of this drawback has to do with refs. If we go a ref as a prop, React will not be capable to inform whether or not or not we have mutated it because the final render. And so it chooses to re-render, to be on the secure aspect.

React’s #1 purpose is to be sure that the UI that the person sees is saved “in sync” with the applying state. And so, React will err on the aspect of too many renders. It does not need to danger exhibiting the person a stale UI.

So, to return to our false impression: props don’t have anything to do with re-renders. Our <BigCountNumber> part is not re-rendering as a result of the rely prop modified.

When a part re-renders, as a result of considered one of its state variables has been up to date, that re-render will cascade all the way in which down the tree, to ensure that React to fill within the particulars of this new sketch, to seize a brand new snapshot.

That is the usual working process, however there is a option to tweak it a bit.

Hyperlink to this heading

Creating pure elements

You could be aware of React.memo, or the React.PureComponent class part. These two instruments permit us to ignore sure re-render requests.

Here is what it seems to be like:

By wrapping our Ornament part with React.memo, we’re telling React “Hey, I know that this part is pure. You need not re-render it except its props change.”

This makes use of a method referred to as memoization.

It is lacking the R, however we will sorta consider it as “memorization”. The concept is that React will keep in mind the earlier snapshot. If not one of the props have modified, React will re-use that stale snapshot reasonably than going by way of the difficulty of producing a model new one.

Let’s suppose I wrap each BigCountNumber and Ornament with the React.memo helper. Here is how this could have an effect on the re-renders:


Props: { rely }

When rely modifications, we re-render Counter, and React will attempt to render each descendant elements.

As a result of BigCountNumber takes rely as a prop, and since that prop has modified, BigCountNumber is re-rendered. However as a result of Ornament‘s props have not modified (on account of it not having any), the authentic snapshot is used as a substitute.

I wish to faux that React.memo is a bit like a lazy photographer. When you ask it to take 5 images of the very same factor, it’s going to take 1 photograph and offer you 5 copies of it. The photographer will solely snap a brand new image when your directions change.

Here is a live-code model, if you would like to poke at it your self. Every memoized part has a console.data name added, so you’ll be able to see within the console precisely when every part renders:

Code Playground

import React from 'react';

perform Ornament() {
  console.data('Ornament render');
  return (
    <div className="ornament">

export default React.memo(Ornament);

You could be questioning: why is not this the default behaviour?? Is not this what we wish, more often than not? Certainly we would enhance efficiency if we skipped rendering elements that do not must be rendered?

I feel as builders, we are inclined to overestimate how costly re-renders are. Within the case of our Ornament part, re-renders are lightning fast.

If a part has a bunch of props and never plenty of descendants, it could actually really be slower to examine if any of the props have modified in comparison with re-rendering the part.

And so, it will be counter-productive to memoize each single part we create. React is designed to seize these snapshots actually shortly! However in particular circumstances, for elements with plenty of descendants or elements that do a ton of inside work, this helper might help fairly a bit.

Hyperlink to this heading

What about context?

We have not talked in any respect about context but, however fortuitously, it does not complicate these items an excessive amount of.

By default, all descendants of a part will re-render if that part’s state modifications. And so, it does not actually change something if we offer that state to all descendants by way of context; both approach, these elements are gonna re-render!

Now by way of pure elements, context is sorta like “invisible props”, or possibly “inside props”.

Let’s take a look at an instance. Right here we have now a pure part that consumes a UserContext context:

On this instance, GreetUser is a pure part with no props, however it has an “invisible” or “inside” dependency: the person being saved in React state, and handed round by way of context.

If that person state variable modifications, a re-render will happen, and GreetUser will generate a brand new snapshot, reasonably than counting on a stale image. React can inform that this part is consuming this specific context, and so it treats it as if it was a prop.

It is more-or-less equal to this:

Play with a dwell instance:

Code Playground

import React from 'react';

const UserContext = React.createContext();

perform UserProvider({ youngsters }) {
  const [user, setUser] = React.useState(null);

  React.useEffect(() => {
    window.setTimeout(() => {
      setUser({ title: 'Kiara' });
    }, 1000)
  }, [])

  return (
    <UserContext.Supplier worth={person}>

perform App() {
  return (
      <GreetUser />

const GreetUser = React.memo(() => {
  const person = React.useContext(UserContext);

  if (!person) {
    return "Hello there!";

  return `Hi there ${person.title}!`;

export default App;

Observe that this solely occurs if the pure part consumes the context with the React.useContext hook. You do not have to fret about context breaking a bunch of pure elements that do not attempt to eat it.

When you’ve labored with React for some time, you’ve got doubtless had the irritating expertise of attempting to determine why a specific part is re-rendering. In a real-world state of affairs, it typically is not apparent in any respect! Luckily, the React Devtools might help.

First, you will must obtain the React Devtools browser extension. It is at present obtainable for Chrome and Firefox. For the needs of this tutorial, I will assume you are utilizing Chrome, although the directions will not differ a lot.

Pop open the devtools with Ctrl + Alt + I (or + Possibility + I on MacOS). It’s best to see two new tabs seem:

Screenshot showing two new tabs in the Chrome devtools, “Components” and “Profiler”

We’re within the “Profiler”. Choose that tab.

Click on the little gear icon, and allow the choice labeled “Report why every part rendered whereas profiling”:

The overall circulation seems to be like this:

  1. Begin recording by hitting the little blue “document” circle.

  2. Carry out some actions in your software.

  3. Cease recording.

  4. View the recorded snapshots to study extra about what occurred.

Every render is captured as a separate snapshot, and you may flick thru them utilizing the arrows. The details about why a part rendered is accessible within the sidebar:

By clicking by way of to the part you are all for, you’ll be able to see precisely why a specific part re-rendered. Within the case of a pure part, it can tell us which prop(s) are liable for this replace.

I do not personally use this instrument typically, however after I do, it is a lifesaver!

Hyperlink to this heading

Highlighting re-renders

Yet another little trick: the React profiler has an choice the place you’ll be able to spotlight elements that re-render.

Here is the setting in query:

screenshot of the React profiler settings, showcasing a setting that reads “Highlight updates when components render”

With this setting enabled, it’s best to see inexperienced rectangles flash round elements that re-render:

This might help us perceive precisely how far-reaching state updates are, and take a look at whether or not our pure elements are efficiently avoiding re-rendering!

One of many issues that you’re going to discover if you begin utilizing the profiler: typically, pure elements re-render even when nothing seems to have modified!

One of many refined mind-bending issues about React is that elements are JavaScript capabilities. Once we render a part, we’re calling the perform.

Because of this something outlined inside a React part is re-created on each single render.

As a fast instance, take into account this:

Each single time we render this App part, we’re producing a model new object. This will wreck havoc on our pure elements; this DogProfile baby goes to re-render whether or not or not we wrap it with React.memo!

In a pair weeks, I will be publishing a “Half II” to this weblog submit. We’ll dig into two famously-inscrutable React hooks, useMemo and useCallback. And we’ll see tips on how to use them to resolve this drawback.

I even have a confession to make: these tutorials have been plucked straight from my upcoming course, “The Pleasure of React”.

I have been constructing with React for over 7 years now, and I’ve discovered rather a lot about tips on how to use it successfully. I completely love working with React; I’ve tried nearly each front-end framework beneath the solar, and nothing makes me really feel as productive as React.

In “The Pleasure of React”, we’ll construct a psychological mannequin for the way React actually works, digging into ideas like we have now on this tutorial. Not like the posts on this weblog, nonetheless, my programs use a “multi-modality” strategy, mixing written content material like this with video content material, workouts, interactive explorables, and even some minigames!

I am trying ahead to pre-launching my course within the subsequent couple months. When you’re , you’ll be able to join updates on the course homepage. 💖

Efficiency optimization in React is a large subject, and I might simply write a number of weblog posts about it. Hopefully, this tutorial has helped construct a strong basis upon which you’ll find out about React efficiency!

That stated, I will share a number of fast suggestions I’ve discovered about React efficiency optimization:

  • The React Profiler exhibits the variety of milliseconds {that a} render took, however this quantity is not reliable. We typically profile issues in “improvement mode”, and React is a lot, a lot sooner in “manufacturing mode”. To actually perceive how performant your software is, it’s best to measure utilizing the “Efficiency” tab in opposition to the deployed manufacturing software. This can present you real-world numbers not only for re-renders, but additionally the format/paint modifications.

  • I strongly advocate testing your functions on lower-end {hardware}, to see what the Ninetieth-percentile expertise is like. It will rely on the product you are constructing, however for this weblog, I periodically take a look at issues on a Xiaomi Redmi 8, a finances smartphone fashionable in India a number of years in the past. I shared this expertise on Twitter.
  • Lighthouse efficiency scores are not an correct reflection of true person expertise. I belief the qualitative expertise of utilizing the applying rather more than the stats proven by any automated instrument.

  • I gave a chat a number of years in the past at React Europe all about efficiency in React! It focuses extra on the “post-load” expertise, an space plenty of builders neglect. You may watch it on YouTube.
  • Do not over-optimize! It is tempting, when studying in regards to the React profiler, to go on an optimization spree, with the purpose of lowering the # of renders as a lot as attainable… however truthfully, React is already very optimized out of the field. These instruments are finest used in response to a efficiency drawback, if issues begin feeling a bit sluggish.



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments