
Templates are easier to statically analyze due to their more deterministic syntax. This makes it easier to reuse existing HTML snippets, apply accessibility best practices, style with CSS, and for designers to understand and modify. So why does Vue recommend templates by default? There are a number of reasons:
DOM CYCLE PARTS FULL
Render functions are more flexible than templates when dealing with highly dynamic logic, because you can work with vnodes using the full power of JavaScript. Vue also provides APIs that allow us to skip the template compilation step and directly author render functions. Vue templates are compiled into virtual DOM render functions. The runtime renderer walks the new tree, compares it with the old one, and applies necessary updates to the actual DOM. This time, a new, updated Virtual DOM tree is created. Patch: When a dependency used during mount changes, the effect re-runs. This step is performed as a reactive effect, so it keeps track of all reactive dependencies that were used. Mount: The runtime renderer invokes the render functions, walks the returned virtual DOM tree, and creates actual DOM nodes based on it. This step can be done either ahead-of-time via a build step, or on-the-fly by using the runtime compiler. Render Pipeline Īt the high level, this is what happens when a Vue component is mounted:Ĭompile: Vue templates are compiled into render functions: functions that return virtual DOM trees.

The main benefit of virtual DOM is that it gives the developer the ability to programmatically create, inspect and compose desired UI structures in a declarative way, while leaving the direct DOM manipulation to the renderer. This process is called patch, also known as "diffing" or "reconciliation". If we have two copies of virtual DOM trees, the renderer can also walk and compare the two trees, figuring out the differences, and apply those changes to the actual DOM. It also contains more children vnodes, which makes it the root of a virtual DOM tree.Ī runtime renderer can walk a virtual DOM tree and construct a real DOM tree from it.

It contains all the information that we need to create the actual element. Here, vnode is a plain JavaScript object (a "virtual node") representing a element.
