
· 3 min read · Angular, React, Change detection
Change Detection in Modern Web Applications
How can we ensure the UI state is always in sync with the application state?
Contents
Overview
Change detection is a fundamental concept in modern web frameworks. It answers the question: How can we ensure the UI state is always in sync with the application state? Each framework employs its methodology to track changes in the application state and update the UI accordingly.
This document explores the concept of change detection, explains why it is necessary, and shows how different frameworks (React and Angular) approach the problem.
Application State vs. UI State
When developing a web application, two core components need to be managed:
- Application State: This is the data representing the current status of your application user data, settings, fetched API responses, etc.
- Transformation Equation: A mechanism (often defined by your chosen framework) that describes how the application state should be rendered as a user interface. We can think of this as: UI = f(application state)
Changes in the application state trigger the need to re-run this transformation to keep the UI in sync. The framework's job is to manage the state and perform this transformation to generate a UI.
Why Change Detection?
A naive approach to updating the UI would be to discard and re-render the entire UI every time something changes. However, this is highly inefficient for large applications with numerous DOM elements. Complete re-rendering is expensive in terms of performance and risks losing important browser-managed states (e.g., cursor focus, text selection).
Instead, frameworks aim to identify only the parts of the UI that have changed the “delta” (ΔUI)—and update only those specific elements. This targeted update preserves performance and maintains the user’s experience.
DOM and Regenerating the UI
DOM Complexity
Modern web pages can contain thousands of DOM nodes. Regenerating the entire DOM structure after every minor update would be prohibitively expensive, both in terms of performance and user experience.
Browser State
The DOM is also stateful in ways the application might not explicitly track (e.g., input focus, cursor position, temporary form values). Discarding the DOM regularly can cause bad user experiences, such as resetting text inputs while the user is typing.
The Solution: ΔUI
To avoid these issues, frameworks calculate and apply a minimal set of changes often referred to as ΔUI. This process ensures that only the necessary parts of the interface are updated.
Change Detection in React and Angular
React
Key Concept: The Virtual DOM
How It Works:
- A React component is essentially a function that takes the application state as input.
- The function returns a virtual representation of the UI (the Virtual DOM).
- React compares this Virtual DOM to the actual DOM.
- The difference (ΔUI) indicates which elements have changed and need updating.
In short: ΔUI=Virtual DOM−Real DOM
Angular
Key Concept: Bindings that are expressed in the component templates
How It Works:
- Angular tracks change in component properties (state changes).
- The framework evaluates bindings in the template to see which values have changed.
- Only those UI elements bound to updated data are changed accordingly.
In short: ΔUI=Changed Data Bindings in the Template
Summary
- Change Detection ensures the UI accurately reflects the current state of the application.
- Naive Approach: Completely rebuild the UI on every change (inefficient and disruptive).
- Optimized Approach: Only update the elements that have changed (ΔUI).
- React: Uses a Virtual DOM and a diffing algorithm to compute changes.
- Angular: Uses binding checks to identify and apply necessary updates.
Final Thoughts
Change detection is a central pillar of modern web development. By efficiently determining the ΔUI, frameworks like React and Angular deliver responsive, user-friendly experiences without incurring the heavy cost of re-rendering the entire DOM or losing crucial browser-managed states. Understanding these mechanisms helps developers write more efficient, maintainable, and predictable applications.