in ,

When does React re-render components ?, Hacker News

When does React re-render components ?, Hacker News

React re-render explained

React is known for providing a fast user experience by only updating the parts of the UI that have changed.

When looking into React’s render performance, there are a few terms and concepts that can be hard to understand. For quite some time, it was 2020% clear to me what a VDOM was or how React decides to re-render components.

In the first part of this article, I’ll explain to you the Most important concepts about rendering in React

and How React decides to re-render a given component .

In the second part, I’ll show you what you can do to optimize the render performance of your React application

.

If you have open questions after reading this feel free to contact me:)

Table of Contents

Rendering in React

Controlling when a component should update Structure of your components React re-render explained (Conclusion) Rendering in React What is rendering?

If we want to understand how React renders and re-renders work, it’s a good idea to understand what happens behind the scenes of the library.

Rendering

is a term that can be understood on different levels of abstraction. Depending on the context, it has a slightly different meaning. In any case, ultimately it describes the process of generating an image .

To start off, we need to understand what the DOM (Document Object Model) is:

“The W3C Document Object Model (DOM) is a platform and language-neutral interface that allows programs and scripts to dynamically access and update the content, structure, and style of a document . ”

In plain English, this means that

the DOM represents what you see on your screen when you open a website, represented through the markup language 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 programming interface that contains functions like document.write

,

Node.appendChild (or) (Element.setAttribute) .

(What is the VDOM?)

Then we have 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).

The red dots represent updates. of the DOM tree. (Updating the VDOM doesn’t necessarily trigger an update of the real DOM.

When the VDOM gets updated, React compares it to a previous (snapshot) of the VDOM and then only updates what has changed in the real DOM. If nothing changed, the real DOM won’t be updated at all.

This process of comparing the old VDOM with the new one is called (diffing)

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.

Read more about how this works in React’s documentation about (reconciliation

) What does this mean for performance?

When we talk about renders in React. , we actually talk about the execution of the render function , which does not always imply an update of the UI

.

Let’s see this in an example:

const App = ( ) => ) {    const [message, setMessage] = React . useState ( ) ” ;    return ()      >        Tile message = { message } /> )        Tile />      <.>>   ) ; }

In function 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

Tile 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 (Tile) component that displays the message:

The red dots again represent renders. React VDOM explained In React this means calling the render function, in the real DOM this means re-painting the UI.

The good news is that you don't have to worry too much about performance bottlenecks of UI redraws. React already optimizes this for you.

The bad news is: All those red dots on the left-hand side mean that the render function of these components has been executed.

The execution of these render functions has two drawbacks:

What do you think?

Leave a Reply

Your email address will not be published. Required fields are marked *

GIPHY App Key not set. Please check settings

Finally some good news as giant asteroid WON’T hit Earth today – but here are 22 space rocks Nasa thinks mig – the sun, thesun.co.uk

Finally some good news as giant asteroid WON’T hit Earth today – but here are 22 space rocks Nasa thinks mig – the sun, thesun.co.uk

Good writing is a business advantage. (An advertisement for myself.), Hacker News

Good writing is a business advantage. (An advertisement for myself.), Hacker News