1 ) 概述
Mode
组件是 react提供给我们的原生组件
ConcurrentMode
和 StrictMode
updateMode
的方法reconcileChildren
去创建children就可以了2 )源码
定位到 packages/react-reconciler/src/ReactFiberBeginWork.js#L349
进入 updateMode
function updateMode(
current: Fiber | null,
workInProgress: Fiber,
renderExpirationTime: ExpirationTime,
) {
const nextChildren = workInProgress.pendingProps.children;
reconcileChildren(
current,
workInProgress,
nextChildren,
renderExpirationTime,
);
return workInProgress.child;
}
reconcileChildren
里面的寻找答案
reconcileChildFibers
再随便选一个api进入,比如 reconcileSingleElement
createFiberFromElement
// packages/react-reconciler/src/ReactFiber.js#L520
export function createFiberFromElement(
element: ReactElement,
mode: TypeOfMode,
expirationTime: ExpirationTime,
): Fiber {
let owner = null;
if (__DEV__) {
owner = element._owner;
}
const type = element.type;
const key = element.key;
const pendingProps = element.props;
const fiber = createFiberFromTypeAndProps(
type,
key,
pendingProps,
owner,
mode,
expirationTime,
);
if (__DEV__) {
fiber._debugSource = element._source;
fiber._debugOwner = element._owner;
}
return fiber;
}
const created = createFiberFromElement(
element,
returnFiber.mode, // 这里
expirationTime,
);
returnFiber
对应着 updateMode
中的 current 或 workInProgresscreateFiberFromElement
createFiberFromTypeAndProps
fiber = createFiber(fiberTag, pendingProps, key, mode);
returnFiber.mode
对应 REACT_CONCURRENT_MODE_TYPE
,参考case REACT_CONCURRENT_MODE_TYPE:
return createFiberFromMode(
pendingProps,
mode | ConcurrentMode | StrictMode, // 这里就是,最终结果: 0b011
expirationTime,
key,
);
returnFiber.mode
对应 REACT_STRICT_MODE_TYPE
, 参考case REACT_STRICT_MODE_TYPE:
return createFiberFromMode(
pendingProps,
mode | StrictMode, // 这里就是,最终结果: 0b010
expirationTime,
key,
);