/
githubmirror
/
preact
Обзор
Документация
Войти
/
githubmirror
/
preact
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
11.0.0-beta.0
src/render.js
69 строк
2 KB
Jovi De Croock
Golf in render.js
25 июл 2025, 00:01
25 июл 2025, 00:01
be691db
Код
Авторство
О чём код?
import { EMPTY_OBJ, MODE_HYDRATE, NULL } from './constants'; import { commitRoot, diff } from './diff/index'; import { createElement, Fragment } from './create-element'; import options from './options'; import { slice } from './util'; /** * Render a Preact virtual node into a DOM element * @param {import('./internal').ComponentChild} vnode The virtual node to render * @param {import('./internal').PreactElement} parentDom The DOM element to render into */ export function render(vnode, parentDom) { // https://github.com/preactjs/preact/issues/3794 if (parentDom == document) { parentDom = document.documentElement; } if (options._root) options._root(vnode, parentDom); // @ts-expect-error let isHydrating = !!(vnode && vnode._flags & MODE_HYDRATE); // To be able to support calling `render()` multiple times on the same // DOM node, we need to obtain a reference to the previous tree. We do // this by assigning a new `_children` property to DOM nodes which points // to the last rendered tree. By default this property is not present, which // means that we are mounting a new tree for the first time. let oldVNode = isHydrating ? NULL : parentDom._children; vnode = parentDom._children = createElement(Fragment, NULL, [vnode]); // List of effects that need to be called after diffing. let commitQueue = [], refQueue = []; diff( parentDom, // Determine the new vnode tree and store it on the DOM element on // our custom `_children` property. vnode, oldVNode || EMPTY_OBJ, EMPTY_OBJ, parentDom.namespaceURI, oldVNode ? NULL : parentDom.firstChild ? slice.call(parentDom.childNodes) : NULL, commitQueue, oldVNode ? oldVNode._dom : parentDom.firstChild, isHydrating, refQueue, parentDom.ownerDocument ); // Flush all queued effects commitRoot(commitQueue, vnode, refQueue); } /** * Update an existing DOM element with data from a Preact virtual node * @param {import('./internal').ComponentChild} vnode The virtual node to render * @param {import('./internal').PreactElement} parentDom The DOM element to update */ export function hydrate(vnode, parentDom) { // @ts-expect-error vnode._flags |= MODE_HYDRATE; render(vnode, parentDom); }