react component info stack
// 在开发中通过组件信息获取所有者堆栈
export function getOwnerStackByComponentInfoInDev(
componentInfo: ReactComponentInfo,
): string {
if (!__DEV__) {
return '';
}
try {
let info = '';
// The owner stack of the current component will be where it was created, i.e. inside its owner.
// There's no actual name of the currently executing component. Instead, that is available
// on the regular stack that's currently executing. However, if there is no owner at all, then
// there's no stack frame so we add the name of the root component to the stack to know which
// component is currently executing.
//
// 当前组件的所有者堆栈将位于它被创建的地方,即在其所有者内部。当前正在执行的组件没有实际的名称。相反,该信息可以在
// 当前正在执行的常规堆栈上获取。但是,如果根本没有所有者,那么就没有堆栈帧,所以我们将根组件的名称添加到堆栈中,以便
// 知道当前正在执行的是哪个组件。
if (!componentInfo.owner && typeof componentInfo.name === 'string') {
return describeBuiltInComponentFrame(componentInfo.name);
}
let owner: void | null | ReactComponentInfo = componentInfo;
while (owner) {
const ownerStack: ?Error = owner.debugStack;
if (ownerStack != null) {
// Server Component
// 服务器组件
owner = owner.owner;
if (owner) {
// TODO: Should we stash this somewhere for caching purposes?
// 待办:我们是否应该将其存放在某处以进行缓存?
info += '\n' + formatOwnerStack(ownerStack);
}
} else {
break;
}
}
return info;
} catch (x) {
return '\nError generating stack: ' + x.message + '\n' + x.stack;
}
}