Skip to content

Commit 66e5e3e

Browse files
committed
Review feedback v11 (#4655)
* Revert assign and avoid repeating indexed access * Check parentNode instead * Use flag * Remove type * Remove todo file
1 parent b0a2b8e commit 66e5e3e

File tree

5 files changed

+26
-31
lines changed

5 files changed

+26
-31
lines changed

TODO.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/diff/index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ export function diff(
249249
c.state = c._nextState;
250250

251251
if (c.getChildContext != NULL) {
252-
globalContext = assign({}, globalContext, c.getChildContext());
252+
globalContext = assign(assign({}, globalContext), c.getChildContext());
253253
}
254254

255255
if (isClassComponent && !isNew && c.getSnapshotBeforeUpdate != NULL) {
@@ -305,9 +305,8 @@ export function diff(
305305
newVNode._dom = oldDom;
306306
} else {
307307
for (let i = excessDomChildren.length; i--; ) {
308-
if (excessDomChildren[i]) {
309-
excessDomChildren[i].remove();
310-
}
308+
const child = excessDomChildren[i];
309+
if (child) child.remove();
311310
}
312311
}
313312
} else {
@@ -564,7 +563,8 @@ function diffElementNodes(
564563
// Remove children that are not part of any vnode.
565564
if (excessDomChildren != NULL) {
566565
for (i = excessDomChildren.length; i--; ) {
567-
if (excessDomChildren[i]) excessDomChildren[i].remove();
566+
const child = excessDomChildren[i];
567+
if (child) child.remove();
568568
}
569569
}
570570
}
@@ -663,7 +663,7 @@ export function unmount(vnode, parentVNode, skipRemove) {
663663
}
664664
}
665665

666-
if (!skipRemove && vnode._dom != null && typeof vnode.type != 'function') {
666+
if (!skipRemove && vnode._dom != null && vnode._dom.parentNode) {
667667
vnode._dom.remove();
668668
}
669669

src/index.d.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -299,16 +299,6 @@ interface ContainerNode {
299299
}
300300

301301
export function render(vnode: ComponentChild, parent: ContainerNode): void;
302-
/**
303-
* @deprecated Will be removed in v11.
304-
*
305-
* Replacement Preact 10+ implementation can be found here: https://gist.github.com/developit/f4c67a2ede71dc2fab7f357f39cff28c
306-
*/
307-
export function render(
308-
vnode: ComponentChild,
309-
parent: ContainerNode,
310-
replaceNode?: Element | Text
311-
): void;
312302
export function hydrate(vnode: ComponentChild, parent: ContainerNode): void;
313303
export function cloneElement(
314304
vnode: VNode<any>,

src/render.js

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { EMPTY_OBJ, NULL } from './constants';
1+
import { EMPTY_OBJ, MODE_HYDRATE, NULL } from './constants';
22
import { commitRoot, diff } from './diff/index';
33
import { createElement, Fragment } from './create-element';
44
import options from './options';
@@ -8,21 +8,17 @@ import { slice } from './util';
88
* Render a Preact virtual node into a DOM element
99
* @param {import('./internal').ComponentChild} vnode The virtual node to render
1010
* @param {import('./internal').PreactElement} parentDom The DOM element to render into
11-
* @param {import('./internal').PreactElement | object} [replaceNode] Optional: Attempt to re-use an
12-
* existing DOM tree rooted at `replaceNode`
1311
*/
14-
export function render(vnode, parentDom, replaceNode) {
12+
export function render(vnode, parentDom) {
1513
// https://github.com/preactjs/preact/issues/3794
1614
if (parentDom == document) {
1715
parentDom = document.documentElement;
1816
}
1917

2018
if (options._root) options._root(vnode, parentDom);
2119

22-
// We abuse the `replaceNode` parameter in `hydrate()` to signal if we are in
23-
// hydration mode or not by passing the `hydrate` function instead of a DOM
24-
// element..
25-
let isHydrating = replaceNode === hydrate;
20+
// @ts-expect-error
21+
let isHydrating = !!(vnode && vnode._flags & MODE_HYDRATE);
2622

2723
// To be able to support calling `render()` multiple times on the same
2824
// DOM node, we need to obtain a reference to the previous tree. We do
@@ -65,5 +61,7 @@ export function render(vnode, parentDom, replaceNode) {
6561
* @param {import('./internal').PreactElement} parentDom The DOM element to update
6662
*/
6763
export function hydrate(vnode, parentDom) {
68-
render(vnode, parentDom, hydrate);
64+
// @ts-expect-error
65+
vnode._flags |= MODE_HYDRATE;
66+
render(vnode, parentDom);
6967
}

src/util.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
import { EMPTY_ARR } from './constants';
22

33
export const isArray = Array.isArray;
4-
export const assign = Object.assign;
54
export const slice = EMPTY_ARR.slice;
5+
6+
/**
7+
* Assign properties from `props` to `obj`
8+
* @template O, P The obj and props types
9+
* @param {O} obj The object to copy properties to
10+
* @param {P} props The object to copy properties from
11+
* @returns {O & P}
12+
*/
13+
export function assign(obj, props) {
14+
// @ts-expect-error We change the type of `obj` to be `O & P`
15+
for (let i in props) obj[i] = props[i];
16+
return /** @type {O & P} */ (obj);
17+
}

0 commit comments

Comments
 (0)