Virtual DOM chay' Qu'
"Virtual DOM React nom" - Hoch vay' jatlh 'ach yajbe'. pegh pagh, 'ej reH nom pagh. real DOM tlhap rap. Qagh choH: real DOM minimal changes tu' (UI state wa' vo' pIm), SoH manual compute pagh.
Qagh Qotlh
direct DOM manipulation Huch: DOM change Hoch Qu' law' - styles recalculate, layout, repaint. UI state choH 'ach nuq pIm Sov pagh vaj naive: Hoch Qaw' 'ej rebuild:
function render(state) {
container.innerHTML = ''; // wipes everything, loses input focus/scroll position too
container.innerHTML = renderHtml(state);
}
Qu' 'ach waste 'ej destructive - form inputs focus Qaw', scroll position reset, CSS transitions restart. neH: DOM changed parts neH choH, pIm touch pagh.
virtual DOM node nuq
virtual DOM node plain JavaScript object ghaH, element chay' legh. real DOM API call pagh.
// Roughly what React.createElement('button', { className: 'btn' }, 'Save') produces
const vnode = {
type: 'button',
props: { className: 'btn' },
children: ['Save'],
};
objects tree chenmoH Dun - object allocation neH, browser rendering engine pagh. Dun 'oH Hoch: render Hoch new tree chenmoH, Huch pagh.
Diffing: old tree new tree rap legh
state choHDI' framework chu' virtual tree chenmoH, old tree rap node-by-node legh. reconciliation pong. simplified algorithm:
function diff(oldVNode, newVNode) {
if (oldVNode.type !== newVNode.type) {
return { type: 'REPLACE', newVNode };
}
const propPatches = diffProps(oldVNode.props, newVNode.props);
const childPatches = diffChildren(oldVNode.children, newVNode.children);
return { type: 'UPDATE', propPatches, childPatches };
}
function diffProps(oldProps, newProps) {
const patches = [];
for (const key in newProps) {
if (oldProps[key] !== newProps[key]) {
patches.push({ key, value: newProps[key] });
}
}
for (const key in oldProps) {
if (!(key in newProps)) {
patches.push({ key, value: undefined }); // removed
}
}
return patches;
}
output new DOM pagh - patches list mach: "attribute choH", "text node choH", "element chel/Qaw'". patches neH real DOM Daq lo': element.setAttribute(), textContent =, appendChild().
lists keys qatlh
children list array index rap diff - items reorder, insert, remove vaj Qagh. stable identity pagh vaj algorithm position 0 old = position 0 new (item choH 'ach).
<!-- Without keys: reordering "B, A, C" makes the diff think every item's
text content changed, when really only the order changed -->
<li>A</li>
<li>B</li>
<li>C</li>
// With a stable key, the algorithm can tell "this specific item moved"
// instead of "everything here is now different"
items.map((item) => ({ type: 'li', key: item.id, children: [item.name] }));
virtual-DOM framework list rendering Hoch key poQ. formality pagh - algorithm Sov laHbe' information.
Huch
full virtual tree chenmoH 'ej diff Huch ghaj - UI mach pagh update shape Sov vaj direct DOM nom laH (tree building 'ej comparison pagh). virtual DOM ghaj nom pagh: declarative programming model ("state tree render") 'ej real DOM neH poQ Daq tlhap - choHta' manual track pagh.
framework virtual DOM diffing neH strategy pagh. Svelte, Angular template compilation: runtime diffing pagh, build time direct update instructions chenmoH - discover pagh, template vo' Sov.
yaj
virtual DOM plain-object UI state description, Dun chenmoH 'ej compare. cha' tree diff minimal patch list nob, real DOM tlhap. "DOM law' nom" pagh - declarative rendering model nob, full rebuild Huch pagh.