Home >Web Front-end >JS Tutorial >DOM Manipulation from Simple Updates to Virtual DOM
When building web applications, one of the most fundamental challenges developers face is efficiently updating the Document Object Model (DOM) in response to user interactions and state changes. Let's explore this concept by examining three different implementations of a todo application, each showcasing a different approach to DOM manipulation.
Github: https://github.com/sreeharsha-rav/javascript_projects/blob/main/todo-app/README.md
In our first implementation, we used the most straightforward approach: directly manipulating the DOM whenever a change occurred. Here's a simplified version of how we handled adding a new todo:
function addTodo() { const todoText = input.value; const todoElement = document.createElement('div'); todoElement.textContent = todoText; todoList.appendChild(todoElement); }
This approach is intuitive and works well for simple applications, but it has limitations:
Our second implementation introduced the concept of state management. Instead of directly manipulating the DOM, we maintained a central state and updated the DOM based on that state:
const todoStore = new Map(); function addTodo(text) { // Update state const id = nextId++; todoStore.set(id, { text, completed: false }); // Update DOM const todoElement = createTodoElement(text, id); todoList.appendChild(todoElement); }
This approach brought several improvements:
However, we still faced a challenge: ensuring our DOM updates were efficient and maintaining consistency between our state and the UI.
Our final implementation introduced a simple virtual DOM-like mechanism using state diffing. This approach represents a significant leap in sophistication:
let todoState = []; // Current state let oldTodoState = []; // Previous state function updateState(newTodos) { // Find what changed const added = newTodos.filter(newTodo => !oldTodoState.some(oldTodo => oldTodo.id === newTodo.id) ); const deleted = oldTodoState.filter(oldTodo => !newTodos.some(newTodo => newTodo.id === oldTodo.id) ); const updated = // ... find updated items // Update only what changed added.forEach(todo => addTodoElement(todo)); deleted.forEach(todo => removeTodoElement(todo)); updated.forEach(todo => updateTodoElement(todo)); // Save current state for next comparison oldTodoState = [...newTodos]; }
The concept of reconciliation, popularized by React, is about efficiently updating the DOM to match our desired state. Our simple implementation demonstrates three key aspects:
We maintain two versions of our state: current and previous. This allows us to detect exactly what changed between updates. The process of finding these differences is called "diffing."
Instead of rebuilding everything, we only update what changed:
All our DOM updates happen in a single pass, after we've calculated all the differences. This is more efficient than making updates one at a time as changes occur.
As we progressed through these implementations, we gained several advantages:
While our implementation is simplified, it demonstrates the core concepts behind modern frontend frameworks like React. The actual Virtual DOM implementation in React is more sophisticated, handling:
The evolution of our todo application shows how different approaches to DOM manipulation come with different trade-offs. While direct manipulation is simpler to understand and implement, a more structured approach with state management and smart updates leads to more maintainable and scalable applications.
The concepts of reconciliation and diffing, even in their simplified form, demonstrate why modern frameworks like React are so powerful. They handle these complex updates automatically, allowing developers to focus on building features rather than optimizing DOM updates.
As web applications become more complex, understanding these fundamental concepts becomes increasingly important. Whether you're using a framework or building something from scratch, knowing how and why DOM updates work the way they do will make you a more effective developer.
The above is the detailed content of DOM Manipulation from Simple Updates to Virtual DOM. For more information, please follow other related articles on the PHP Chinese website!