Skip to content

Commit 756f8a7

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 eaf7ce9 commit 756f8a7

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
@@ -248,7 +248,7 @@ export function diff(
248248
c.state = c._nextState;
249249

250250
if (c.getChildContext != null) {
251-
globalContext = assign({}, globalContext, c.getChildContext());
251+
globalContext = assign(assign({}, globalContext), c.getChildContext());
252252
}
253253

254254
if (isClassComponent && !isNew && c.getSnapshotBeforeUpdate != null) {
@@ -300,9 +300,8 @@ export function diff(
300300
newVNode._dom = oldDom;
301301
} else {
302302
for (let i = excessDomChildren.length; i--; ) {
303-
if (excessDomChildren[i]) {
304-
excessDomChildren[i].remove();
305-
}
303+
const child = excessDomChildren[i];
304+
if (child) child.remove();
306305
}
307306
}
308307
} else {
@@ -544,7 +543,8 @@ function diffElementNodes(
544543
// Remove children that are not part of any vnode.
545544
if (excessDomChildren != null) {
546545
for (i = excessDomChildren.length; i--; ) {
547-
if (excessDomChildren[i]) excessDomChildren[i].remove();
546+
const child = excessDomChildren[i];
547+
if (child) child.remove();
548548
}
549549
}
550550
}
@@ -643,7 +643,7 @@ export function unmount(vnode, parentVNode, skipRemove) {
643643
}
644644
}
645645

646-
if (!skipRemove && vnode._dom != null && typeof vnode.type != 'function') {
646+
if (!skipRemove && vnode._dom != null && vnode._dom.parentNode) {
647647
vnode._dom.remove();
648648
}
649649

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 } from './constants';
1+
import { EMPTY_OBJ, MODE_HYDRATE } 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)