Member-only story
React JS — Understanding Rerender in depth
In React, a “rerender” refers to the process of re-rendering a component and its children in the Virtual DOM when the component’s state or props change. Understanding how a rerender works is crucial for building efficient and performant web applications with React. In this article, we’ll explore in-depth what a rerender is, how it works, and provide an example to illustrate the concept.
What is a Rerender in React?
In React, when a component’s state or props change, React automatically triggers a rerender of that component and its children in the Virtual DOM. During a rerender, React updates the Virtual DOM with the new state and props of the component and then calculates the minimal number of changes required to update the real DOM.
How Does a Rerender Work?
When a component’s state or props change, React creates a new Virtual DOM tree that represents the updated state and props of the component. React then compares this new Virtual DOM tree with the previous Virtual DOM tree to determine the minimum number of changes required to update the real DOM.
React uses a process called “diffing” to calculate the minimal number of changes required to update the real DOM. Diffing involves comparing the new Virtual DOM tree with the previous…