跳到主要内容

React 捕获的值

一、作用

二、在 Fiber 上创建捕获值

备注
export function createCapturedValueAtFiber<T>(
value: T,
source: Fiber,
): CapturedValue<T> {
// If the value is an error, call this function immediately after it is thrown
// so the stack is accurate.
//
// 如果值是错误,在抛出后立即调用此函数
// 以确保堆栈信息准确。
if (typeof value === 'object' && value !== null) {
const existing = CapturedStacks.get(value);
if (existing !== undefined) {
return existing;
}
const captured: CapturedValue<T> = {
value,
source,
stack: getStackByFiberInDevAndProd(source),
};
CapturedStacks.set(value, captured);
return captured;
} else {
return {
value,
source,
stack: getStackByFiberInDevAndProd(source),
};
}
}

三、从错误创建捕获值

export function createCapturedValueFromError(
value: Error,
stack: null | string,
): CapturedValue<Error> {
const captured = {
value,
source: null,
stack: stack,
};
if (typeof stack === 'string') {
CapturedStacks.set(value, captured);
}
return captured;
}

四、常量

1. 捕获的栈

const CapturedStacks: WeakMap<any, CapturedValue<any>> = new WeakMap();