DEV Community

Sourav Bandyopadhyay
Sourav Bandyopadhyay

Posted on

What is Reconciliation in ReactJS?

By comparing the former and present states of a component's tree and identifying what has changed, the reconciliation process in React enables the framework to update the user interface quickly.

React needs to update the DOM to reflect changes in a component's state or props. To completely create the DOM from scratch each time there is an update would be ineffective. Instead, React employs a procedure called reconciliation to ascertain which parts of the DOM require updating.

React searches for changes between the old and new versions of a component's tree throughout the reconciliation process. So, rather than recreating the entire tree, it merely updates the DOM elements that have changed.

Let's say you have a component that displays a list of items, and the state of that list changes. When the state changes, React will perform a reconciliation to update the DOM with the new items.

Here's an example code snippet:

import React, { useState, useEffect } from "react";


function Reconciliation({ data }) {
  const [items, setItems] = useState([]);

  useEffect(() => {
    setItems(data);
  }, [data]);

  const handleDelete = (id) => {
    const filteredItems = items.filter((item) => item.id !== id);
    setItems(filteredItems);
  };

  return (
    <div>
      <h2>Reconciliation</h2>
      <ul>
        {items.map((item) => (
          <li key={item.id}>
            {item.name}{" "}
            <button onClick={() => handleDelete(item.id)}>Delete</button>
          </li>
        ))}
      </ul>
    </div>
  );
}

export default Reconciliation;
Enter fullscreen mode Exit fullscreen mode

This is an example of reconciliation because the component updates its internal state to reflect changes in the data prop, and re-renders to reflect changes in the items state.

Top comments (0)