React Re-rendering Components
Tags: development, javascript, react
Published: 2020-05-01 03:36:47

---
title: 'React Re-rendering Components'
published: true
description: 'React is known for providing a fast user experience by only updating the parts of the UI that have changed. In this article, we discuss how we can optimise the render performance by understanding how React decides to re-render'
tags: ['development', 'javascript', 'react']
---

![](https://seanamarasinghe.com/static/70cf4ded27448bba0780efb003004289/14b42/feature.jpg)

## What is components re-rendering?

When looking into React's render performance, there are a few terms and concepts that can be hard to understand.

Here, we will look at the most important concepts about rendering in React and how React decides to re-render a given component.

And then we will find out what we can do to optimise the render performance of your React application.

### Document Object Model (DOM)

To understand how React renders and re-renders work, we can peek into React codebase to see what happens behind the scenes of the library.

The tree-like DOM represents the structure of a website, represented by HTML. 
JavaScript also has a DOM, which is represented as an object where the root element is the document.

You can modify the DOM with JavaScript through the DOM API that contains functions like `document.write`, `Node.appendChild` or `Element.setAttribute`.

### What is the Virtual document Object Model (VDOM)?

Then there's the Virtual DOM (or VDOM) of React, which is another abstraction layer on top of that. It consists of your React application's elements.

State changes in your application will be applied to the VDOM first. If the new state of the VDOM requires a UI change, 
the ReactDOM library will efficiently do this by trying to update only what needs to be updated.

For example, if only the attribute of an element changes, React will only update the attribute of the HTML element by calling `document.setAttribute` (or something similar).

Real DOM updates are slow because they cause an actual re-draw of the UI. React makes this more efficient by updating the smallest amount possible in the real DOM.

Therefore we have to be aware of the difference between native and virtual DOM updates.

![Virtual DOM](https://seanamarasinghe.com/images/2020-04-30-react-re-rendering-components/vdom.jpg)

## Performance

When we talk about renders in React, we talk about the execution of the render function, which doesn't always imply an update of the UI.

Let's see this in an example:

```js
const App = () => {
  const [message, setMessage] = React.useState('');
  return (
    <>
      <Info message={message} />
      <Info />
    </>
  );
};
```
In functional components, the execution of the whole function is the equivalent of the render function in class components.

When the state changes in the higher-order component (HOC, in this case, App), the two Info components will re-render, even though the second one doesn't even receive any props.

This translates to having the render function being called 3 times, but actual DOM modifications only happen once in the Info component that displays the message.

React already optimises this for you, so you don't have to worry too much about performance bottlenecks of UI redraws.

The execution of these render functions has two drawbacks:

* React has to run its diffing algorithm on each of those components to check whether it should update the UI.
* All your code in these render functions or function components will be executed again.

The first point is arguably not that important since React manages to calculate the difference quite efficiently. 
The danger lies in the code that you wrote is being executed over and over on every React render.

In the example above we have a really small component tree. But imagine what happens if each node has more children and these again might have child-components. 
We'll see how we can optimize this.

To read further [go here](http://localhost:8000/developer/2020-03-30-react-re-rendering-components)