DEV Community

Sourav Bandyopadhyay
Sourav Bandyopadhyay

Posted on

1

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.

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay