DEV Community

Sean Saranga Amarasinghe
Sean Saranga Amarasinghe

Posted on

React Re-rendering Components

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

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:

const App = () => {
  const [message, setMessage] = React.useState('');
  return (
    <>
      <Info message={message} />
      <Info />
    </>
  );
};
Enter fullscreen mode Exit fullscreen mode

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

Top comments (1)

Collapse
 
sandeep_kamra_db541a51c0f profile image
Sandeep Kamra

Kindly add a correct link in the description.