Open gnosis23 opened 5 years ago
首先来暖场的:
然后稍微提高一点难度:
更高阶一点的问题:
带着问题阅读
答案就在:
// stack
ReactMultiChild._updateChildren ( diff 算法:插入、移动、删除 )
|-ReactMultiChild_reconcilerUpdateChildren( 将children放到map里 )
|--ReactChildReconciler.updateChildren ( 新增、更新、删除 )
默认会给孩子赋一个 key
// src/shared/utils/traverseAllChildren.js
function getComponentKey(component, index) {
// Do some typechecking here since we call this blindly. We want to ensure
// that we don't block potential future ES APIs.
if (component && typeof component === 'object' && component.key != null) {
// Explicit key
return KeyEscapeUtils.escape(component.key);
}
// Implicit key determined by the index in the set
return index.toString(36);
}
问题2的答案:
instantiateReactComponent
实例化成 ReactCompositeComponent(class/function)、ReactHostComponent(dom)、Empty等等内部实例ReactElement
(JSX结构) 和 instance(props 和 state)。分别通过 mountComponent
、receiveComponent
、updateComponent
等方法来维护原理是:
_pendingStateQueue
里只有标记为dirty的component会渲染
参考这篇文章 。
// 非真实
interface FiberNode {
// root | host | class
tag: FiberType;
// user defined class | DOM tag
type?: typeof Component | string;
// class instance | DOMElement
stateNode?: FiberDOMElement | Text | Component;
props: object;
child?: FiberNode;
sibling?: FiberNode;
parent?: FiberNode;
// old tree
alternate?: FiberNode;
// PLACEMENT | UPDATE | DELETION
effectTag?: EffectTag;
// fibers
effects?: FiberNode[];
partialState?: object;
}
Version
15: old 16+: Fiber
Fiber