React Repaint: What Happens After An Update?

does react re-paint all elements after update

React is a powerful tool for building user interfaces that aims to minimize the amount of work needed to keep the user interface (UI) in sync with the application state. When a component's state changes, React will re-render that component and all its child components. This process is recursive, and React will only update the Document Object Model (DOM) nodes that have changed between renders. This is achieved through a process called reconciliation, where React compares the current version of the virtual DOM to the previous version to identify what needs to be updated. While this approach can lead to more renders than necessary, it ensures that the user doesn't see an outdated UI.

Characteristics Values
Does React re-render all elements within a component after an update? Yes, React will re-render all elements within a component after an update. However, it only makes changes to the DOM nodes if there is a difference between renders. It does this by comparing the current version of the virtual DOM to the previous version and only updates the elements that have changed.
How does React know what to re-render? React uses a diffing algorithm to check which elements have changed and then updates the fiber tree to reflect these changes. It also uses the virtual DOM, which is compared to the real DOM on every render, to determine the minimal number of operations needed to achieve the new state of the UI.
What triggers a re-render? A re-render is triggered when a component's state is updated. This can be done using the set function, which automatically queues a re-render.
Does React update the DOM directly? No, React is told what the DOM should look like and then makes the necessary changes.

cypaint

React's rendering process

In the initial render, React starts from the root component and builds a React element tree of what the DOM should look like. For subsequent renders, React calls the function component whose state update triggered the render. This recursive process continues until all necessary components are rendered. During this phase, React also performs a reconciliation process, where it compares the new virtual DOM with the previous render to identify the changes needed.

The commit phase is where React applies the calculated changes to the actual DOM. React modifies the DOM nodes only if there are differences between renders. It uses a process called "diffing" to identify the changes between the old and new trees of React elements, ensuring that only the necessary updates are made to the UI. This minimises DOM updates and improves performance by avoiding costly repaints of the entire page.

After updating the DOM, React updates all refs and runs specific lifecycle methods and hooks. Finally, the browser repaints the screen to reflect the changes, which is known as "browser rendering" or "painting".

Overall, React's rendering process aims to efficiently update the user interface by minimising DOM manipulations and optimising the rendering process to provide a smooth and responsive user experience.

cypaint

Reconciliation process

React's rendering process can be split into two phases: the reconciliation process and the commitment phase. The reconciliation process involves calling every component in the application and comparing the returned HTML to the previous render process. This comparison is done between the virtual DOM and the real DOM, and React will then make the minimal necessary changes to the real DOM to match the latest rendering output. This process is recursive, with React rendering any returned components one after the other.

The reconciliation process can be further broken down into several steps. Firstly, a new React Elements Tree (Virtual DOM) is created. Then, a diffing algorithm is run on the fiber tree to check which elements have changed. The fiber tree is another tree constructed by React on App load. After this, the new React tree is compared to the previous React tree to check if any DOM elements have been added or removed, and these changes are then made to the DOM.

The commitment phase comes after the render phase and involves creating a special list of changes that should be applied to the real DOM. React will only change the DOM nodes if there is a difference between renders. This is done to minimise DOM updates and achieve the new state of the UI with the minimal number of operations.

It is important to note that a component re-render will cascade down the tree, causing all potentially affected components and child components to re-render as well. This is because React will err on the side of too many renders to avoid showing the user a stale UI.

cypaint

DOM manipulation

React does not re-paint all elements after an update. It only changes DOM nodes if there is a difference between renders. It does the minimal amount of real DOM manipulation. After rendering (calling) your components, React will modify the DOM.

JavaScript can manipulate this tree structure, allowing developers to dynamically alter the content and appearance of a webpage. It can access and change all the elements of an HTML document. It can also change all the HTML attributes and CSS styles on a page. Additionally, JavaScript can remove existing HTML elements and attributes, as well as add new ones.

cypaint

Virtual DOM

React does not repaint all elements after an update. Instead, it uses a virtual DOM to efficiently update the browser's main Document Object Model (DOM) by comparing virtual copies. The virtual DOM is a performance optimization technique used by JavaScript frameworks like React. It is a lightweight copy of the real DOM that is updated in-memory before the changes are applied to the actual DOM. This allows React to calculate and apply only the necessary changes to the real DOM, avoiding a full repaint.

The virtual DOM is a programming concept where an ideal or "virtual" representation of a UI is kept in memory and synced with the "real" DOM by a library such as ReactDOM. This process is called reconciliation. React's reconciliation process efficiently updates the UI based on changes in the virtual DOM. When a component's state is updated, React creates a new virtual DOM tree to reflect the updated state. It then compares this new virtual DOM tree with the previous one using a diffing algorithm to identify the minimal set of changes required.

By minimizing direct interactions with the real DOM, React significantly reduces rendering time. It only updates the DOM nodes if there is a difference between renders. This approach enables the declarative API of React. Developers tell React what state they want the UI to be in, and React ensures that the DOM matches that state. This abstracts out the attribute manipulation, event handling, and manual DOM updating that would otherwise be required.

The virtual DOM also provides benefits such as improved security, better extensibility, cross-browser consistency, and seamless integration with React's component-based architecture. It was first introduced by React in 2013 and has since been adopted by other web frameworks like Vue.js and Inferno. However, it has also been criticized for being slow due to the additional time required for diffing and reconciling DOM nodes, leading to the development of frameworks without a virtual DOM, such as Svelte.

cypaint

Optimising performance

React does not repaint all elements after an update. When a state change occurs, React will re-render the component and its child components. However, React aims to minimise DOM updates by only making changes to the DOM nodes that have actually changed between renders. This is achieved through a process called reconciliation, where React compares the current version of the virtual DOM to the previous version to identify differences. This allows React to perform the minimal necessary operations to update the DOM and improve performance.

To optimise performance, it is important to understand how React renders and updates components. React uses a virtual DOM, which is a lightweight representation of the real DOM. When a component's state is updated, React creates a new virtual DOM and compares it to the previous version using a diffing algorithm. This process helps identify which elements have changed and need to be updated in the real DOM. By minimising the number of DOM manipulations, React improves performance and ensures a smooth user experience.

One way to optimise performance is to avoid unnecessary re-renders. While React will automatically re-render components when their state changes, it is important to note that re-rendering child components can be costly in terms of performance. To prevent unnecessary re-renders, you can use techniques such as memoisation or the React.memo() function to control which components are re-rendered. Additionally, you can use the shouldComponentUpdate() lifecycle method to manually decide whether a component should re-render based on your specific use case.

Another optimisation technique is to use the React.PureComponent base class instead of the default React.Component. PureComponent includes a shallow prop and state comparison, which helps prevent unnecessary re-renders by default. This can be particularly useful when dealing with large component trees, as it reduces the number of DOM manipulations and improves performance.

It is also important to consider the structure of your component tree. If a component high up in the tree triggers a re-render, it can cause all of its child components to re-render as well. This can be inefficient, especially if the child components have not actually changed. To optimise performance, try to keep components that trigger re-renders low in the tree, so that the impact on child components is minimised. Additionally, you can use techniques such as code splitting or lazy loading to further optimise the rendering process and improve performance.

By understanding how React handles rendering and updates, developers can implement various optimisation techniques to improve the performance of their applications. By minimising unnecessary re-renders, using efficient base classes, and structuring the component tree thoughtfully, developers can ensure that their React applications are fast and responsive, providing a seamless user experience.

Frequently asked questions

Yes, React will re-render all elements within a component after an update. However, it only modifies the DOM nodes that have changed between renders. This is achieved by comparing the current version of the virtual DOM to the previous version and updating only the elements that are different. This minimises DOM updates.

No, React does not directly update the DOM. Instead, it is told what the DOM should look like, and it makes the necessary changes.

A component will re-render when its state variables are updated. This will cascade down the tree, causing all child components to re-render as well.

Written by
Reviewed by

Explore related products

Share this post
Print
Did this article help you?

Leave a comment