React - Basic

1. How does React handle state management?

  • state represents the data that can change over time within a component.
  • Using state, React provides a straightforward and declarative way to manage and update data within components.

2. What is the virtual DOM, and how does React use it?

  • Virtual DOM is a lightweight copy of the actual DOM
  • By using the virtual DOM, React avoids the inefficiencies of directly manipulating the actual DOM for every state change.
  • Instead, it calculates the optimal updates and applies them selectively, resulting in improved performance and a more efficient rendering process.

a. Initial render:

  • When a React component is first rendered, it creates a virtual DOM representation of the component hierarchy.
  • Basically is a JavaScript object that mirrors the structure of the actual DOM.

b. Reconciliation:

  • Whenever a component's state or props change, React performs a process called reconciliation.
  • React compares the previous virtual DOM with the new virtual DOM to identify the differences between them.

b. Diffing:

  • The reconciliation process involves a diffing algorithm.
  • It efficiently determines the minimum number of changes needed to update the actual DOM based on the changes in the virtual DOM.