Rendering Mechanism
Vue compiles templates into efficient JavaScript render functions. Learn how the virtual DOM and reactivity system work together to update the UI efficiently.
How Rendering Works
Section titled “How Rendering Works”-
Template Compilation
Vue templates are compiled into JavaScript render functions. These functions return virtual DOM trees describing what should be rendered. -
Virtual DOM
The virtual DOM is a lightweight JavaScript representation of the actual DOM. When state changes, Vue creates a new virtual DOM tree and efficiently compares it to the previous one (diffing). -
Patching
Vue calculates the minimal set of changes needed and updates only the affected parts of the real DOM (patching), improving performance. -
Reactivity System
Vue tracks dependencies during rendering. When reactive data changes, only the components that depend on that data are re-rendered.
Example
Section titled “Example”<template> <p>{{ message }}</p></template><script setup>import { ref } from "vue";const message = ref("Hello Vue!");</script>When message changes, Vue automatically re-renders only the affected part of the DOM.
Optimization Tips
Section titled “Optimization Tips”- Use
keywithv-forfor efficient list updates. - Avoid unnecessary reactive dependencies in templates.
- Use computed properties for expensive calculations.